Skip to content

Commit

Permalink
Add: config hub route
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Jun 20, 2018
1 parent c2c8f82 commit 05ab653
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 4 deletions.
18 changes: 18 additions & 0 deletions constant/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,26 @@ const (

type RuleType int

func (rt RuleType) String() string {
switch rt {
case DomainSuffix:
return "DomainSuffix"
case DomainKeyword:
return "DomainKeyword"
case GEOIP:
return "GEOIP"
case IPCIDR:
return "IPCIDR"
case FINAL:
return "FINAL"
default:
return "Unknow"
}
}

type Rule interface {
RuleType() RuleType
IsMatch(addr *Addr) bool
Adapter() string
Payload() string
}
67 changes: 67 additions & 0 deletions hub/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package hub

import (
"net/http"

"github.com/go-chi/chi"
"github.com/go-chi/render"
)

type Configs struct {
Proxys []Proxy `json:"proxys"`
Rules []Rule `json:"rules"`
}

type Proxy struct {
Name string `json:"name"`
}

type Rule struct {
Name string `json:"name"`
Payload string `json:"type"`
}

func configRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getConfig)
r.Put("/", updateConfig)
return r
}

func getConfig(w http.ResponseWriter, r *http.Request) {
rulesCfg, proxysCfg := tun.Config()

var (
rules []Rule
proxys []Proxy
)

for _, rule := range rulesCfg {
rules = append(rules, Rule{
Name: rule.RuleType().String(),
Payload: rule.Payload(),
})
}

for _, proxy := range proxysCfg {
proxys = append(proxys, Proxy{Name: proxy.Name()})
}

w.WriteHeader(http.StatusOK)
render.JSON(w, r, Configs{
Rules: rules,
Proxys: proxys,
})
}

func updateConfig(w http.ResponseWriter, r *http.Request) {
err := tun.UpdateConfig()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, Error{
Error: err.Error(),
})
return
}
w.WriteHeader(http.StatusNoContent)
}
6 changes: 4 additions & 2 deletions hub/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewHub(addr string) {

r.Get("/traffic", traffic)
r.Get("/logs", getLogs)
r.Mount("/configs", configRouter())

err := http.ListenAndServe(addr, r)
if err != nil {
Expand All @@ -43,7 +44,7 @@ func NewHub(addr string) {
}

func traffic(w http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusOK)
w.WriteHeader(http.StatusOK)

tick := time.NewTicker(time.Second)
t := tun.Traffic()
Expand All @@ -64,10 +65,11 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
sub, err := src.Subscribe()
defer src.UnSubscribe(sub)
if err != nil {
render.Status(r, http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, Error{
Error: err.Error(),
})
return
}
render.Status(r, http.StatusOK)
for elm := range sub {
Expand Down
4 changes: 4 additions & 0 deletions rules/domain_keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (dk *DomainKeyword) Adapter() string {
return dk.adapter
}

func (dk *DomainKeyword) Payload() string {
return dk.keyword
}

func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
return &DomainKeyword{
keyword: keyword,
Expand Down
4 changes: 4 additions & 0 deletions rules/domain_suffix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (ds *DomainSuffix) Adapter() string {
return ds.adapter
}

func (ds *DomainSuffix) Payload() string {
return ds.suffix
}

func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
return &DomainSuffix{
suffix: suffix,
Expand Down
4 changes: 4 additions & 0 deletions rules/final.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (f *Final) Adapter() string {
return f.adapter
}

func (f *Final) Payload() string {
return ""
}

func NewFinal(adapter string) *Final {
return &Final{
adapter: adapter,
Expand Down
4 changes: 4 additions & 0 deletions rules/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (g *GEOIP) Adapter() string {
return g.adapter
}

func (g *GEOIP) Payload() string {
return g.country
}

func NewGEOIP(country string, adapter string) *GEOIP {
return &GEOIP{
country: country,
Expand Down
8 changes: 6 additions & 2 deletions rules/ipcidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ func (i *IPCIDR) IsMatch(addr *C.Addr) bool {
return i.ipnet.Contains(*addr.IP)
}

func (g *IPCIDR) Adapter() string {
return g.adapter
func (i *IPCIDR) Adapter() string {
return i.adapter
}

func (i *IPCIDR) Payload() string {
return i.ipnet.String()
}

func NewIPCIDR(s string, adapter string) *IPCIDR {
Expand Down
4 changes: 4 additions & 0 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (t *Tunnel) Traffic() *C.Traffic {
return t.traffic
}

func (t *Tunnel) Config() ([]C.Rule, map[string]C.Proxy) {
return t.rules, t.proxys
}

func (t *Tunnel) Log() *observable.Observable {
return t.observable
}
Expand Down

0 comments on commit 05ab653

Please sign in to comment.