-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for running as a service on Windows
Signed-off-by: Brian Dwyer <[email protected]>
- Loading branch information
1 parent
e20c11b
commit 5463e2f
Showing
7 changed files
with
123 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !windows | ||
|
||
package ntlm_proxy | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
func RunWindows() { | ||
log.Fatal("RunWindows should never be called on a platform other than Windows!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// +build windows | ||
|
||
// Windows Service Handler | ||
|
||
package ntlm_proxy | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
|
||
"github.com/kardianos/service" | ||
) | ||
|
||
type program struct { | ||
exit chan struct{} | ||
} | ||
|
||
var svcFlag string | ||
|
||
func init() { | ||
if flag.Lookup("service") == nil { | ||
flag.StringVar(&svcFlag, "service", "", "Control the Windows System service.") | ||
} | ||
} | ||
|
||
func RunWindows() { | ||
svcConfig := &service.Config{ | ||
Name: "gontlm-proxy", | ||
DisplayName: "GoNTLM Proxy", | ||
Description: "GoNTLM Forwarding Proxy", | ||
Arguments: []string{"-proxy", proxyServer}, | ||
} | ||
|
||
prg := &program{} | ||
s, err := service.New(prg, svcConfig) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if len(svcFlag) != 0 { | ||
err := service.Control(s, svcFlag) | ||
if err != nil { | ||
log.Printf("Valid actions: %q\n", service.ControlAction) | ||
log.Fatal(err) | ||
} | ||
return | ||
} | ||
|
||
if err = s.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func (p *program) Start(s service.Service) (err error) { | ||
if service.Interactive() { | ||
log.Println("Running in terminal.") | ||
} else { | ||
log.Println("Running under service manager.") | ||
} | ||
p.exit = make(chan struct{}) | ||
|
||
go p.run() | ||
|
||
return | ||
} | ||
|
||
func (p *program) run() (err error) { | ||
// Run the Proxy | ||
go Run() | ||
// Wait for Exit Signal | ||
for { | ||
select { | ||
case <-p.exit: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (p *program) Stop(s service.Service) (err error) { | ||
close(p.exit) | ||
return | ||
} |