From 53208448ae7ee3ca55a537b7a5988e71d2d4b537 Mon Sep 17 00:00:00 2001 From: halulu Date: Wed, 28 Nov 2018 12:05:44 +0800 Subject: [PATCH] remove client app --- client/app.go | 113 -------------------------------------------------- client/web.go | 44 -------------------- websocks.go | 14 ------- 3 files changed, 171 deletions(-) delete mode 100644 client/app.go delete mode 100644 client/web.go diff --git a/client/app.go b/client/app.go deleted file mode 100644 index 8d43921..0000000 --- a/client/app.go +++ /dev/null @@ -1,113 +0,0 @@ -package client - -import ( - "bytes" - "fmt" - "io" - "net/http" - "os" - "os/exec" - - "encoding/json" - "io/ioutil" - - "github.com/go-macaron/pongo2" - "github.com/sirupsen/logrus" - "gopkg.in/macaron.v1" -) - -type App struct { - //todo - WebListenAddr string - - m macaron.Macaron - - running bool - //todo multiple client - *WebSocksClient -} - -func LoadApp() (app *App, err error) { - app = &App{} - data, err := ioutil.ReadFile("client.json") - if err != nil { - return - } - - err = json.Unmarshal(data, app) - if err != nil { - return - } - return -} - -func NewApp() (app *App) { - app = &App{ - WebListenAddr: ":10801", - } - return -} - -func (app *App) Save() (err error) { - data, err := json.MarshalIndent(app, "", " ") - if err != nil { - return - } - - err = ioutil.WriteFile("client.json", data, 0600) - return -} - -func (app *App) Run() (err error) { - //log setup - buf := make([]byte, 0) - buffer := bytes.NewBuffer(buf) - log.Out = io.MultiWriter(os.Stdout, buffer) - log.SetLevel(logrus.DebugLevel) - - m := macaron.New() - m.Use(pongo2.Pongoer()) - m.Get("/", func(ctx *macaron.Context) { - ctx.HTML(200, "client/client") - return - }) - - //todo pac - m.Get("/pac", func(ctx *macaron.Context) { - return - }) - - //api v0 - m.Group("/api/v0/client", func() { - m.Get("/log", func(ctx *macaron.Context) { - ctx.WriteHeader(200) - ctx.Write(buffer.Bytes()) - return - }) - m.Get("/stats", func(ctx *macaron.Context) { - if app.WebSocksClient == nil { - ctx.Error(403, "websocks client is not running") - return - } - ctx.JSON(200, app.WebSocksClient.Stats) - }) - m.Post("/start", app.StartClient) - m.Post("/stop", app.StopClient) - }) - - go func() { - err := exec.Command("explorer", fmt.Sprintf("http://127.0.0.1%s", app.WebListenAddr)).Run() - if err != nil { - log.Debug(err.Error()) - return - } - }() - - log.Infof("web start to listen at %s", app.WebListenAddr) - err = http.ListenAndServe(app.WebListenAddr, m) - if err != nil { - log.Error(err.Error()) - return - } - return -} diff --git a/client/web.go b/client/web.go deleted file mode 100644 index 36d4137..0000000 --- a/client/web.go +++ /dev/null @@ -1,44 +0,0 @@ -package client - -import ( - "encoding/json" - "io/ioutil" - - "gopkg.in/macaron.v1" -) - -func (app *App) StartClient(ctx *macaron.Context) { - config := &Config{} - data, err := ioutil.ReadAll(ctx.Req.Body().ReadCloser()) - if err != nil { - ctx.Error(403, err.Error()) - } - - err = json.Unmarshal(data, config) - if err != nil { - ctx.Error(403, err.Error()) - } - - websocksClient, err := config.GetClient() - if err != nil { - ctx.Error(403, err.Error()) - } - - app.WebSocksClient = websocksClient - app.running = true - - go func() { - err = websocksClient.Run() - if err != nil { - log.Error(err.Error()) - } - }() - return -} - -func (app *App) StopClient(ctx *macaron.Context) { - app.WebSocksClient.Stop() - ctx.WriteHeader(200) - ctx.Write([]byte("stopped")) - return -} diff --git a/websocks.go b/websocks.go index b59db71..903032b 100644 --- a/websocks.go +++ b/websocks.go @@ -28,20 +28,6 @@ func main() { Description: "websocks.org", Author: "Halulu", Email: "lzjluzijie@gmail.com", - Action: func(c *cli.Context) (err error) { - app, err := client.LoadApp() - if err != nil { - log.Println("can not load client.json, create not one") - app = client.NewApp() - err = app.Save() - if err != nil { - log.Printf("save config: %s", err.Error()) - } - } - - err = app.Run() - return - }, Commands: []cli.Command{ { Name: "client",