Skip to content

Commit

Permalink
feature: support cascade tcp proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldin95 committed Mar 22, 2020
1 parent 58622da commit d703993
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 64 deletions.
15 changes: 11 additions & 4 deletions http/api/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ func (le DHCPLease) Router(router *mux.Router) {
router.HandleFunc("/api/dhcp/lease", le.GET).Methods("GET")
}

func (le DHCPLease) GET(w http.ResponseWriter, r *http.Request) {
func (le DHCPLease) Get(data map[string]schema.DHCPLease) error {
leases, err := libvirtn.ListLeases()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return err
}
data := make(map[string]schema.DHCPLease, 128)
for addr, le := range leases {
data[addr] = schema.DHCPLease{
Mac: le.Mac,
Expand All @@ -30,6 +28,15 @@ func (le DHCPLease) GET(w http.ResponseWriter, r *http.Request) {
Type: le.Type,
}
}
return nil
}

func (le DHCPLease) GET(w http.ResponseWriter, r *http.Request) {
data := make(map[string]schema.DHCPLease, 128)
if err := le.Get(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ResponseJson(w, data)
}

Expand Down
97 changes: 60 additions & 37 deletions http/api/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package api

import (
"github.com/danieldin95/lightstar/compute/libvirtc"
"github.com/danieldin95/lightstar/http/client"
"github.com/danieldin95/lightstar/libstar"
"github.com/danieldin95/lightstar/network/libvirtn"
"github.com/danieldin95/lightstar/schema"
"github.com/danieldin95/lightstar/service"
"github.com/gorilla/mux"
"net/http"
"sort"
Expand Down Expand Up @@ -38,71 +39,93 @@ func (pro ProxyTcp) Graphics(inst *schema.Instance) []schema.Target {
return dst
}

func (pro ProxyTcp) Inside(inst *schema.Instance) []schema.Target {
func (pro ProxyTcp) GetTarget(host string, inst *schema.Instance, leases schema.ListLeases) []schema.Target {
dst := make([]schema.Target, 0, 32)
leases, err := libvirtn.ListLeases()
if err != nil {
libstar.Warn("ProxyTcp.Inside %s", err)
return dst
}
for _, inf := range inst.Interfaces {
libstar.Debug("ProxyTcp.GET %s", inf.Address)
if le, ok := leases[inf.Address]; ok {
dst = append(dst, schema.Target{
Name: inst.Name,
Target: le.IPAddr + ":22",
Host: host,
}) // ssh
dst = append(dst, schema.Target{
Name: inst.Name,
Target: le.IPAddr + ":3389",
Host: host,
}) // rdp
break
}
}
return dst
}

func (pro ProxyTcp) Remote(inst *schema.Instance) []schema.Target {
dst := make([]schema.Target, 0, 32)
leases, err := libvirtn.ListLeases()
func (pro ProxyTcp) Local(user *schema.User) []schema.Target {
leases := make(map[string]schema.DHCPLease, 128)
err := DHCPLease{}.Get(leases)
if err != nil {
libstar.Warn("ProxyTcp.Inside %s", err)
return dst
return nil
}
for _, inf := range inst.Interfaces {
libstar.Debug("ProxyTcp.GET %s", inf.Address)
if le, ok := leases[inf.Address]; ok {
dst = append(dst, schema.Target{
Name: inst.Name,
Target: le.IPAddr + ":22",
}) // ssh
dst = append(dst, schema.Target{
Name: inst.Name,
Target: le.IPAddr + ":3389",
}) // rdp
list := schema.List{
Items: make([]interface{}, 0, 32),
}
Instance{}.GetByUser(user, &list)
dst := make([]schema.Target, 0, 32)
for _, item := range list.Items {
inst := item.(schema.Instance)
dst = append(dst, pro.GetTarget("", &inst, leases)...)
}
return dst
}

func (pro ProxyTcp) Remote(user *schema.User) []schema.Target {
dst := make([]schema.Target, 0, 32)
insApi := Instance{}
for zone := range service.SERVICE.Zone.List() {
if zone == nil {
break
}
if zone.Url == "" {
continue
}
cl := client.Client{
Auth: libstar.Auth{
Type: "basic",
Username: zone.Username,
Password: zone.Password,
},
Host: zone.Url,
}
leases := schema.ListLeases{}
err := client.DHCPLease{Client: cl}.Get(&leases)
if err != nil {
libstar.Error("ProxyTcp.Remote.Lease %s", err)
continue
}
var list schema.ListInstance
err = client.Instance{Client: cl}.Get(&list)
if err != nil {
libstar.Error("ProxyTcp.Remote.Instance %s", err)
continue
}
for _, inst := range list.Items {
if !insApi.HasPermission(user, inst.Name) {
continue
}
dst = append(dst, pro.GetTarget(zone.Name, &inst, leases)...)
}
}
return dst
}

func (pro ProxyTcp) GET(w http.ResponseWriter, r *http.Request) {
user, _ := GetUser(r)
list := schema.List{
Items: make([]interface{}, 0, 32),
}

Instance{}.GetByUser(&user, &list)
sort.SliceStable(list.Items, func(i, j int) bool {
return list.Items[i].(schema.Instance).Name < list.Items[j].(schema.Instance).Name
})

tgt := make([]schema.Target, 0, 32)
for _, item := range list.Items {
inst := item.(schema.Instance)
//tgt = append(tgt, pro.Graphics(&inst)...)
tgt = append(tgt, pro.Inside(&inst)...)
}
tgt = append(tgt, pro.Local(&user)...)
tgt = append(tgt, pro.Remote(&user)...)
sort.SliceStable(tgt, func(i, j int) bool {
return (tgt[i].Host + ":" + tgt[i].Name) < (tgt[j].Host + ":" + tgt[j].Name)
})
ResponseJson(w, tgt)
}

Expand Down
1 change: 1 addition & 0 deletions http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Client struct {
Auth libstar.Auth
Host string
}

func (cl Client) NewRequest(url string) *libstar.HttpClient {
Expand Down
12 changes: 6 additions & 6 deletions http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestDHCPLease_Get(t *testing.T) {
Type: "basic",
Username: "admin:123",
},
Host: "https://localhost:10080",
},
Host: "https://localhost:10080",
}
les := map[string]schema.DHCPLease{}
les := schema.ListLeases{}
fmt.Println(api.Get(&les), les)
les = map[string]schema.DHCPLease{}
les = schema.ListLeases{}
api.Client.Auth.Username = "123"
fmt.Println(api.Get(&les), les)
}
Expand All @@ -31,8 +31,8 @@ func TestProxyTcp_Get(t *testing.T) {
Type: "basic",
Username: "admin:123",
},
Host: "https://localhost:10080",
},
Host: "https://localhost:10080",
}
var ps []schema.Target
fmt.Println(api.Get(&ps), ps)
Expand All @@ -45,9 +45,9 @@ func TestInstance_Get(t *testing.T) {
Type: "basic",
Username: "admin:123",
},
Host: "https://localhost:10080",
},
Host: "https://localhost:10080",
}
var data schema.List
var data schema.ListInstance
fmt.Println(api.Get(&data), data)
}
3 changes: 1 addition & 2 deletions http/client/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (

type DHCPLease struct {
Client
Host string
}

func (api DHCPLease) Url() string {
return api.Host + "/api/dhcp/lease"
}

func (api DHCPLease) Get(data *map[string]schema.DHCPLease) error {
func (api DHCPLease) Get(data *schema.ListLeases) error {
client := api.NewRequest(api.Url())
if err := api.GetJSON(client, data); err != nil {
libstar.Error("DHCPLease.Get %s", err)
Expand Down
3 changes: 1 addition & 2 deletions http/client/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type Instance struct {
Client
Host string
Name string
}

Expand All @@ -18,7 +17,7 @@ func (api Instance) Url() string {
return api.Host + "/api/instance/" + api.Name
}

func (api Instance) Get(data *schema.List) error {
func (api Instance) Get(data *schema.ListInstance) error {
client := api.NewRequest(api.Url())
if err := api.GetJSON(client, data); err != nil {
libstar.Error("Instance.Get %s", err)
Expand Down
1 change: 0 additions & 1 deletion http/client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type ProxyTcp struct {
Client
Host string
}

func (api ProxyTcp) Url() string {
Expand Down
40 changes: 39 additions & 1 deletion http/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (t TcpSocket) Router(router *mux.Router) {
router.Handle("/ext/tcpsocket", websocket.Handler(t.Handle))
}

func (t TcpSocket) Handle(ws *websocket.Conn) {
func (t TcpSocket) Local(ws *websocket.Conn) {
defer ws.Close()
ws.PayloadType = websocket.BinaryFrame

Expand Down Expand Up @@ -178,3 +178,41 @@ func (t TcpSocket) Handle(ws *websocket.Conn) {
}()
wait.Wait()
}

func (t TcpSocket) Remote(ws *websocket.Conn) {
r := ws.Request()
host := api.GetQueryOne(r, "host")
if host == "" {
return
}
node := service.SERVICE.Zone.Get(host)
if node == nil {
libstar.Error("host not found: %s", host)
return
}
query := r.URL.Query()
query.Set("host", "")
r.URL.RawQuery = query.Encode()
pri := libstar.ProxyWs{
Proxy: libstar.Proxy{
Server: node.Url,
Auth: libstar.Auth{
Type: "basic",
Username: node.Username,
Password: node.Password,
},
},
}
pri.Initialize()
pri.Socket(ws)
}

func (t TcpSocket) Handle(ws *websocket.Conn) {
r := ws.Request()
host := api.GetQueryOne(r, "host")
if host == "" {
t.Local(ws)
} else {
t.Remote(ws)
}
}
6 changes: 5 additions & 1 deletion libstar/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func (pri *ProxyWs) Dial(url_, protocol, origin string) (ws *websocket.Conn, err
config.Protocol = []string{protocol}
}
config.TlsConfig = pri.TlsConfig

if pri.Auth.Type == "basic" {
config.Header = http.Header{
"Authorization": {BasicAuth(pri.Auth.Username, pri.Auth.Password)},
}
}
return websocket.DialConfig(config)
}

Expand Down
9 changes: 5 additions & 4 deletions lightpix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func GetPorts(host, auth string) (error, []schema.Target) {
Type: "basic",
Username: auth,
},
Host: host,
},
Host: host,
}
return api.Get(&data), data
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func main() {
Type: "basic",
Username: cfg.Auth,
},
Url: cfg.Url + "/ext/tcpsocket?target=",
Url: cfg.Url + "/ext/tcpsocket",
},
}
pri.Initialize()
Expand All @@ -92,8 +92,9 @@ func main() {
input := ""
fmt.Scanln(&input)
for _, tgt := range pri.Target {
if l, ok := pri.Listen[tgt.Target]; ok {
libstar.Info("main %-15s %-20s on %-15s", l.Tgt.Name, l.Tgt.Target, l.Listen)
if l, ok := pri.Listen[tgt.Host+":"+tgt.Target]; ok {
libstar.Info("main %s:%-15s %-20s on %-15s",
l.Tgt.Host, l.Tgt.Name, l.Tgt.Target, l.Listen)
}
}
}
Expand Down
Loading

0 comments on commit d703993

Please sign in to comment.