Last active
February 21, 2020 11:06
-
-
Save sachinsu/78fc6a3b4767df0d45de42f8b140dd4a to your computer and use it in GitHub Desktop.
Web server as system daemon (Windows Service/Systemd) in Go
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
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
// "strconv" | |
"github.com/kardianos/service" | |
) | |
var logger service.Logger | |
type WebServer struct { | |
port string | |
server *http.Server | |
router *http.ServeMux | |
} | |
func main() { | |
if err := run(os.Args, log.Writer()); err != nil { | |
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) | |
os.Exit(0) | |
} | |
} | |
func run(args []string, stdout io.Writer) error { | |
var port = flag.Int("port", 8080, "Specify the port number to use. default is 8080") | |
flag.Parse() | |
svcConfig := &service.Config{ | |
Name: "RemoteFileExec", | |
DisplayName: "Remote file execution service", | |
Description: "Remote file execution service", | |
} | |
prg := &WebServer{} | |
prg.port = fmt.Sprintf(":%d", *port) | |
// log.Printf("%s\n", prg.port) | |
prg.router = http.NewServeMux() | |
prg.routes() | |
s, err := service.New(prg, svcConfig) | |
if err != nil { | |
fmt.Fprintf(stdout, "%s\n", err) | |
return err | |
} | |
logger, err = s.Logger(nil) | |
if err != nil { | |
fmt.Fprintf(stdout, "%s\n", err) | |
return err | |
} | |
err = s.Run() | |
if err != nil { | |
logger.Error(err) | |
return err | |
} | |
return nil | |
} | |
func (p *WebServer) Start(s service.Service) error { | |
// Start should not block. Do the actual work async. | |
go p.run() | |
return nil | |
} | |
func (p *WebServer) run() { | |
p.server = &http.Server{ | |
Addr: p.port, | |
Handler: p.router, | |
} | |
// log.Println("Starting Server") | |
err := p.server.ListenAndServe() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Do work here | |
} | |
func (p *WebServer) Stop(s service.Service) error { | |
// Stop should not block. Return with a few seconds. | |
idleConnsClosed := make(chan struct{}) | |
go func(pg *http.Server, sg service.Service) { | |
if err := pg.Shutdown(context.Background()); err != nil { | |
// Error from closing listeners, or context timeout: | |
logger, err1 := sg.Logger(nil) | |
if err1 == nil { | |
logger.Error(err) | |
} | |
} | |
close(idleConnsClosed) | |
}(p.server, s) | |
<-idleConnsClosed | |
return nil | |
} |
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
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
// "strconv" | |
) | |
func (w *WebServer) routes() { | |
w.router.HandleFunc("/", w.handleIndex()) | |
w.router.HandleFunc("/api/exec", w.handleFileExec()) | |
} | |
func (w *WebServer) handleIndex() func(w http.ResponseWriter, r *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header | |
w.WriteHeader(http.StatusOK) | |
fmt.Fprintf(w, "Remote file Execution service. Pl. use /api/exec?scriptpath=<path to executable>") | |
} | |
} | |
//todo: Path /api/exec | |
//todo: Use https://golang.org/pkg/os/exec/#Command | |
//todo: public ActionResult<string> Get(string scriptpath, bool waitforexit) | |
// ensure that %SYSTEMROOT%\system32 is added to path | |
func (w *WebServer) handleFileExec() func(w http.ResponseWriter, r *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
q := r.URL.Query() | |
spath := q.Get("scriptpath") | |
if _, err := os.Stat(spath); os.IsNotExist(err) { | |
fmt.Fprintf(w, "file does not exist") | |
return | |
} | |
// waitforexit, _ := strconv.ParseBool(q.Get("waitforexit")) | |
cmd := exec.Command("cmd.exe", "/c", spath) | |
// if waitforexit == true { | |
stdoutStderr, err := cmd.CombinedOutput() | |
if err != nil { | |
log.Fatal(err) | |
} | |
w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header | |
w.WriteHeader(http.StatusOK) | |
fmt.Fprintf(w, "%s", stdoutStderr) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment