From 2f93668a4423a8634fb9afdd3688bebcba90455d Mon Sep 17 00:00:00 2001 From: xjdrew Date: Sat, 24 Sep 2016 12:41:26 +0800 Subject: [PATCH] add manager --- example.ini | 3 +++ k1/config.go | 5 ++++ k1/manager.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ k1/one.go | 6 +++++ 4 files changed, 83 insertions(+) create mode 100644 k1/manager.go diff --git a/example.ini b/example.ini index a16bc85..5ab533c 100644 --- a/example.ini +++ b/example.ini @@ -92,3 +92,6 @@ pattern = proxy-country # set to a proxy for domaines that don't match any pattern # DEFAULT VALUE: "" # final = A + +[manager] +listen = "0.0.0.0:9200" diff --git a/k1/config.go b/k1/config.go index 8db04e6..2013c92 100644 --- a/k1/config.go +++ b/k1/config.go @@ -53,6 +53,10 @@ type RuleConfig struct { Final string } +type ManagerConfig struct { + Listen string +} + type KoneConfig struct { General GeneralConfig TCP NatConfig @@ -62,6 +66,7 @@ type KoneConfig struct { Proxy map[string]*ProxyConfig Pattern map[string]*PatternConfig Rule RuleConfig + Manager ManagerConfig } func (cfg *KoneConfig) isValidProxy(proxy string) bool { diff --git a/k1/manager.go b/k1/manager.go new file mode 100644 index 0000000..7481879 --- /dev/null +++ b/k1/manager.go @@ -0,0 +1,69 @@ +package k1 + +import ( + "html/template" + "net/http" +) + +const masterTmpl = ` +{{define "header"}} + + + +{{.Title}} + + + +{{end}} + +{{define "footer"}} + + +{{end}} + +{{define "index"}} +{{template "header" .}} +

List of URLs

+ +{{template "footer" .}} +{{end}} +` + +type Manager struct { + listen string + tmpl *template.Template +} + +func (m *Manager) indexHandle(w http.ResponseWriter, r *http.Request) { + err := m.tmpl.ExecuteTemplate(w, "index", map[string]interface{}{ + "Title": "", + "URLs": []string{ + "/host", + "/destination", + }, + }) + if err != nil { + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } +} + +func (m *Manager) Serve() error { + http.HandleFunc("/", m.indexHandle) + logger.Infof("[manager] listen on: %s", m.listen) + return http.ListenAndServe(m.listen, nil) +} + +func NewManager(cfg ManagerConfig) *Manager { + if cfg.Listen == "" { + return nil + } + return &Manager{ + listen: cfg.Listen, + tmpl: template.Must(template.New("master").Parse(masterTmpl)), + } +} diff --git a/k1/one.go b/k1/one.go index 1454f51..48c0c79 100644 --- a/k1/one.go +++ b/k1/one.go @@ -29,6 +29,7 @@ type One struct { tcpRelay *TCPRelay udpRelay *UDPRelay tun *TunDriver + manager *Manager } func (one *One) Serve() error { @@ -45,6 +46,9 @@ func (one *One) Serve() error { go runAndWait(one.tcpRelay.Serve) go runAndWait(one.udpRelay.Serve) go runAndWait(one.tun.Serve) + if one.manager != nil { + go runAndWait(one.manager.Serve) + } return <-done } @@ -92,5 +96,7 @@ func FromConfig(cfg *KoneConfig) (*One, error) { one.tun.AddRoutes(cfg.Route.V) + // new manager + one.manager = NewManager(cfg.Manager) return one, nil }