forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: selector and proxys & rules router
- Loading branch information
Showing
12 changed files
with
390 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package adapters | ||
|
||
import ( | ||
"errors" | ||
|
||
C "github.com/Dreamacro/clash/constant" | ||
) | ||
|
||
type Selector struct { | ||
name string | ||
selected C.Proxy | ||
proxys map[string]C.Proxy | ||
} | ||
|
||
func (s *Selector) Name() string { | ||
return s.name | ||
} | ||
|
||
func (s *Selector) Type() C.AdapterType { | ||
return C.Selector | ||
} | ||
|
||
func (s *Selector) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { | ||
return s.selected.Generator(addr) | ||
} | ||
|
||
func (s *Selector) Now() string { | ||
return s.selected.Name() | ||
} | ||
|
||
func (s *Selector) All() []string { | ||
var all []string | ||
for k, _ := range s.proxys { | ||
all = append(all, k) | ||
} | ||
return all | ||
} | ||
|
||
func (s *Selector) Set(name string) error { | ||
proxy, exist := s.proxys[name] | ||
if !exist { | ||
return errors.New("Proxy does not exist") | ||
} | ||
s.selected = proxy | ||
return nil | ||
} | ||
|
||
func NewSelector(name string, proxys map[string]C.Proxy) (*Selector, error) { | ||
if len(proxys) == 0 { | ||
return nil, errors.New("Provide at least one proxy") | ||
} | ||
|
||
mapping := make(map[string]C.Proxy) | ||
var init string | ||
for k, v := range proxys { | ||
mapping[k] = v | ||
init = k | ||
} | ||
s := &Selector{ | ||
name: name, | ||
proxys: mapping, | ||
selected: proxys[init], | ||
} | ||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package hub | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
A "github.com/Dreamacro/clash/adapters" | ||
C "github.com/Dreamacro/clash/constant" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/go-chi/render" | ||
) | ||
|
||
func proxyRouter() http.Handler { | ||
r := chi.NewRouter() | ||
r.Get("/", getProxys) | ||
r.Get("/{name}", getProxy) | ||
r.Put("/{name}", updateProxy) | ||
return r | ||
} | ||
|
||
type SampleProxy struct { | ||
Type string `json:"type"` | ||
} | ||
|
||
type Selector struct { | ||
Type string `json:"type"` | ||
Now string `json:"now"` | ||
All []string `json:"all"` | ||
} | ||
|
||
type URLTest struct { | ||
Type string `json:"type"` | ||
Now string `json:"now"` | ||
} | ||
|
||
func transformProxy(proxy C.Proxy) interface{} { | ||
t := proxy.Type() | ||
switch t { | ||
case C.Selector: | ||
selector := proxy.(*A.Selector) | ||
return Selector{ | ||
Type: t.String(), | ||
Now: selector.Now(), | ||
All: selector.All(), | ||
} | ||
case C.URLTest: | ||
return URLTest{ | ||
Type: t.String(), | ||
Now: proxy.(*A.URLTest).Now(), | ||
} | ||
default: | ||
return SampleProxy{ | ||
Type: proxy.Type().String(), | ||
} | ||
} | ||
} | ||
|
||
type GetProxysResponse struct { | ||
Proxys map[string]interface{} `json:"proxys"` | ||
} | ||
|
||
func getProxys(w http.ResponseWriter, r *http.Request) { | ||
_, rawProxys := tun.Config() | ||
proxys := make(map[string]interface{}) | ||
for name, proxy := range rawProxys { | ||
proxys[name] = transformProxy(proxy) | ||
} | ||
render.JSON(w, r, GetProxysResponse{Proxys: proxys}) | ||
} | ||
|
||
func getProxy(w http.ResponseWriter, r *http.Request) { | ||
name := chi.URLParam(r, "name") | ||
_, proxys := tun.Config() | ||
proxy, exist := proxys[name] | ||
if !exist { | ||
w.WriteHeader(http.StatusNotFound) | ||
render.JSON(w, r, Error{ | ||
Error: "Proxy not found", | ||
}) | ||
return | ||
} | ||
render.JSON(w, r, transformProxy(proxy)) | ||
} | ||
|
||
type UpdateProxyRequest struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func updateProxy(w http.ResponseWriter, r *http.Request) { | ||
req := UpdateProxyRequest{} | ||
if err := render.DecodeJSON(r.Body, &req); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
render.JSON(w, r, Error{ | ||
Error: "Format error", | ||
}) | ||
return | ||
} | ||
|
||
name := chi.URLParam(r, "name") | ||
_, proxys := tun.Config() | ||
proxy, exist := proxys[name] | ||
if !exist { | ||
w.WriteHeader(http.StatusNotFound) | ||
render.JSON(w, r, Error{ | ||
Error: "Proxy not found", | ||
}) | ||
return | ||
} | ||
|
||
selector, ok := proxy.(*A.Selector) | ||
if !ok { | ||
w.WriteHeader(http.StatusBadRequest) | ||
render.JSON(w, r, Error{ | ||
Error: "Proxy can't update", | ||
}) | ||
return | ||
} | ||
|
||
if err := selector.Set(req.Name); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
render.JSON(w, r, Error{ | ||
Error: fmt.Sprintf("Selector update error: %s", err.Error()), | ||
}) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} |
Oops, something went wrong.