Skip to content

Commit

Permalink
add manager
Browse files Browse the repository at this point in the history
  • Loading branch information
xjdrew committed Sep 24, 2016
1 parent 532736a commit 2f93668
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example.ini
Expand Up @@ -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"
5 changes: 5 additions & 0 deletions k1/config.go
Expand Up @@ -53,6 +53,10 @@ type RuleConfig struct {
Final string
}

type ManagerConfig struct {
Listen string
}

type KoneConfig struct {
General GeneralConfig
TCP NatConfig
Expand All @@ -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 {
Expand Down
69 changes: 69 additions & 0 deletions k1/manager.go
@@ -0,0 +1,69 @@
package k1

import (
"html/template"
"net/http"
)

const masterTmpl = `
{{define "header"}}
<!DOCTYPE HTML>
<html>
<head>
<title>{{.Title}}</title>
<meta charset='utf-8'>
</head>
<body>
{{end}}
{{define "footer"}}
</body>
</html>
{{end}}
{{define "index"}}
{{template "header" .}}
<h2>List of URLs</h2>
<ul>
{{range .URLs}}
<li><a href='{{.}}'>{{.}}</a></li>
{{end}}
</ul>
{{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)),
}
}
6 changes: 6 additions & 0 deletions k1/one.go
Expand Up @@ -29,6 +29,7 @@ type One struct {
tcpRelay *TCPRelay
udpRelay *UDPRelay
tun *TunDriver
manager *Manager
}

func (one *One) Serve() error {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 2f93668

Please sign in to comment.