Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

Commit

Permalink
archive templates
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjluzijie committed Jul 22, 2018
2 parents 12cdca1 + 0839968 commit 7cfdc58
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ caddy.log
dist
websocks.cer
websocks.key
*.config.json
*.pac
*.json
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ archive:
format_overrides:
- goos: windows
format: zip
wrap_in_directory: true
files:
- LICENSE
- README.md
- README-en.md
- templates/**/*
103 changes: 101 additions & 2 deletions client/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package client

import "gopkg.in/macaron.v1"
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"os/exec"

type WebSocksClientApp struct {
"encoding/json"
"io/ioutil"

"github.com/go-macaron/pongo2"
"github.com/sirupsen/logrus"
"gopkg.in/macaron.v1"
)

type App struct {
//todo
WebListenAddr string

Expand All @@ -12,3 +26,88 @@ type WebSocksClientApp struct {
//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
}
65 changes: 2 additions & 63 deletions client/web.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,13 @@
package client

import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"

"github.com/go-macaron/pongo2"
"github.com/sirupsen/logrus"
"gopkg.in/macaron.v1"
)

func (app *WebSocksClientApp) RunWeb() {
//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")
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", "http://127.0.0.1:10801").Run()
if err != nil {
log.Debug(err.Error())
return
}
}()

log.Infof("web start to listen at :10801")
err := http.ListenAndServe(":10801", m)
if err != nil {
log.Error(err.Error())
return
}
return
}

func (app *WebSocksClientApp) StartClient(ctx *macaron.Context) {
func (app *App) StartClient(ctx *macaron.Context) {
config := &Config{}
data, err := ioutil.ReadAll(ctx.Req.Body().ReadCloser())
if err != nil {
Expand Down Expand Up @@ -97,7 +36,7 @@ func (app *WebSocksClientApp) StartClient(ctx *macaron.Context) {
return
}

func (app *WebSocksClientApp) StopClient(ctx *macaron.Context) {
func (app *App) StopClient(ctx *macaron.Context) {
app.WebSocksClient.Stop()
ctx.WriteHeader(200)
ctx.Write([]byte("stopped"))
Expand Down
6 changes: 0 additions & 6 deletions server.json

This file was deleted.

47 changes: 42 additions & 5 deletions server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"crypto/tls"
"net/http"

"encoding/json"
"io/ioutil"

"github.com/gorilla/sessions"
"gopkg.in/macaron.v1"
)
Expand All @@ -12,21 +15,55 @@ type App struct {
//todo multiple servers
*WebSocksServer

ListenAddr string
TLS bool
CertPath string
KeyPath string
WebListenAddr string
TLS bool
CertPath string
KeyPath string

s http.Server
store sessions.Store
m *macaron.Macaron
}

func LoadApp() (app *App, err error) {
app = &App{}
data, err := ioutil.ReadFile("server.json")
if err != nil {
return
}

err = json.Unmarshal(data, app)
if err != nil {
return
}
return
}

func NewApp() (app *App) {
app = &App{
WebListenAddr: ":23333",
TLS: false,
CertPath: "websocks.cer",
KeyPath: "websocks.key",
}
return
}

func (app *App) Save() (err error) {
data, err := json.MarshalIndent(app, "", " ")
if err != nil {
return
}

err = ioutil.WriteFile("server.json", data, 0600)
return
}

func (app *App) Run() (err error) {
m := app.Macaron()
app.m = m
app.s = http.Server{
Addr: app.ListenAddr,
Addr: app.WebListenAddr,
Handler: m,
}

Expand Down
File renamed without changes.
7 changes: 0 additions & 7 deletions templates/status.html

This file was deleted.

31 changes: 20 additions & 11 deletions websocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"os"

"encoding/json"
"errors"
"io/ioutil"
"os/exec"
"runtime"

"log"

"github.com/juju/loggo"
"github.com/lzjluzijie/websocks/client"
"github.com/lzjluzijie/websocks/core"
Expand All @@ -28,8 +29,17 @@ func main() {
Author: "Halulu",
Email: "[email protected]",
Action: func(c *cli.Context) (err error) {
app := client.WebSocksClientApp{}
app.RunWeb()
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{
Expand Down Expand Up @@ -220,15 +230,14 @@ func main() {
Aliases: []string{"w"},
Usage: "web ui server",
Action: func(c *cli.Context) (err error) {
app := &server.App{}
data, err := ioutil.ReadFile("server.json")
if err != nil {
return
}

err = json.Unmarshal(data, app)
app, err := server.LoadApp()
if err != nil {
return
log.Println("can not load server.json, create not one")
app = server.NewApp()
err = app.Save()
if err != nil {
log.Printf("save config: %s", err.Error())
}
}

err = app.Run()
Expand Down

0 comments on commit 7cfdc58

Please sign in to comment.