@@ -8,14 +8,14 @@ import (
88
99 "github.com/alecthomas/kong"
1010 "github.com/cedws/sshgate/pkg/sshgate"
11- "github.com/fsnotify/fsnotify"
1211)
1312
1413type cli struct {
15- ListenAddr string `help:"Address to listen on" default:":2222"`
16- Ruleless bool `help:"Run in ruleless mode"`
17- LogFormat string `help:"Log format"`
18- Config string `help:"Path to JSON config file"`
14+ ListenAddr string `help:"Address to listen on" default:":2222"`
15+ Ruleless bool `help:"Run in ruleless mode"`
16+ NoConfigReload bool `help:"Disable config reload"`
17+ LogFormat string `help:"Log format"`
18+ Config string `help:"Path to JSON config file"`
1919
2020 Serve serveCmd `cmd:"" default:"1" help:"Start the server"`
2121 JSONSchema jsonschemaCmd `cmd:"" name:"jsonschema" help:"Print config JSON schema"`
@@ -43,74 +43,27 @@ func (s *serveCmd) Run(cli *cli) error {
4343 return err
4444 }
4545
46- if err := serveUntilReload (context .Background (), cli , config ); err != nil && ! errors .Is (err , context .Canceled ) {
46+ if err := serve (context .Background (), cli , config ); err != nil && ! errors .Is (err , context .Canceled ) {
4747 return err
4848 }
4949 }
5050}
5151
52- func serveUntilReload (ctx context.Context , cli * cli , config * sshgate.Config ) error {
53- watcher , err := fsnotify .NewWatcher ()
54- if err != nil {
55- return err
56- }
57- defer watcher .Close ()
58-
59- if err := watcher .Add (cli .Config ); err != nil {
60- return err
61- }
62-
63- for _ , hostKeyPath := range []string {
64- config .HostKeyPaths .ECDSA ,
65- config .HostKeyPaths .ED25519 ,
66- config .HostKeyPaths .RSA ,
67- } {
68- if hostKeyPath != "" {
69- if err := watcher .Add (hostKeyPath ); err != nil {
70- return err
71- }
72- }
73- }
74-
75- ctx , cancel := fsnotifyContext (ctx , watcher )
76- defer cancel ()
77-
78- return serve (ctx , cli , config )
79- }
80-
8152func serve (ctx context.Context , c * cli , config * sshgate.Config ) error {
8253 var opts []sshgate.Option
54+
8355 if c .Ruleless {
8456 opts = append (opts , sshgate .WithRulelessMode ())
8557 }
58+ if ! c .NoConfigReload {
59+ opts = append (opts , sshgate .WithConfigReload ())
60+ }
8661
8762 server := sshgate .New (config , c .ListenAddr , opts ... )
8863
8964 return server .ListenAndServe (ctx )
9065}
9166
92- func fsnotifyContext (ctx context.Context , watcher * fsnotify.Watcher ) (context.Context , context.CancelFunc ) {
93- ctx , cancel := context .WithCancel (ctx )
94-
95- go func () {
96- for {
97- select {
98- case evt := <- watcher .Events :
99- if evt .Op & (fsnotify .Write | fsnotify .Create | fsnotify .Rename ) != 0 {
100- slog .Info ("config file changed, reloading" )
101- cancel ()
102- }
103- case <- watcher .Errors :
104- cancel ()
105- case <- ctx .Done ():
106- return
107- }
108- }
109- }()
110-
111- return ctx , cancel
112- }
113-
11467func Execute () {
11568 var cli cli
11669
0 commit comments