Skip to content

Commit

Permalink
Add websocket function partly & Reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4ntonn committed Mar 30, 2024
1 parent de126db commit e48c50b
Show file tree
Hide file tree
Showing 36 changed files with 319 additions and 143 deletions.
3 changes: 1 addition & 2 deletions admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package main

Expand Down Expand Up @@ -34,7 +33,7 @@ func main() {

share.GeneratePreAuthToken(options.Secret)

protocol.DecideType("raw", options.Downstream)
protocol.SetUpDownStream("raw", options.Downstream)

cli.Banner()

Expand Down
3 changes: 1 addition & 2 deletions admin/admin_win.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build windows
// +build windows

package main

Expand Down Expand Up @@ -29,7 +28,7 @@ func main() {

share.GeneratePreAuthToken(options.Secret)

protocol.DecideType("raw", options.Downstream)
protocol.SetUpDownStream("raw", options.Downstream)

cli.Banner()

Expand Down
5 changes: 2 additions & 3 deletions admin/cli/interactive.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package cli

Expand Down Expand Up @@ -1043,7 +1042,7 @@ func (console *Console) handleNodePanelCommand(uuidNum int) {
}

func (console *Console) handleShellPanelCommand(route string, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -1071,7 +1070,7 @@ func (console *Console) handleShellPanelCommand(route string, uuid string) {
}

func (console *Console) handleSSHPanelCommand(route string, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
6 changes: 3 additions & 3 deletions admin/cli/interactive_win.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build windows
//go:build windows

package cli

Expand Down Expand Up @@ -1038,7 +1038,7 @@ func (console *Console) handleNodePanelCommand(uuidNum int) {
}

func (console *Console) handleShellPanelCommand(route string, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func (console *Console) handleShellPanelCommand(route string, uuid string) {
}

func (console *Console) handleSSHPanelCommand(route string, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
6 changes: 3 additions & 3 deletions admin/handler/backward.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewBackward(lPort, rPort string) *Backward {
}

func (backward *Backward) LetBackward(mgr *manager.Manager, route string, uuid string) error {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
// test if node can listen on assigned port
header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -63,7 +63,7 @@ func (backward *Backward) LetBackward(mgr *manager.Manager, route string, uuid s
}

func (backward *Backward) start(mgr *manager.Manager, topo *topology.Topology, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
// first , admin need to know the route to target node,so ask topo for the answer
topoTask := &topology.TopoTask{
Mode: topology.GETROUTE,
Expand Down Expand Up @@ -214,7 +214,7 @@ func GetBackwardInfo(mgr *manager.Manager, uuid string) (int, bool) {
}

func StopBackward(mgr *manager.Manager, uuid, route string, choice int) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
2 changes: 1 addition & 1 deletion admin/handler/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func LetConnect(mgr *manager.Manager, route, uuid, addr string) error {
return err
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
4 changes: 2 additions & 2 deletions admin/handler/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (forward *Forward) LetForward(mgr *manager.Manager, route string, uuid stri
return err
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -103,7 +103,7 @@ func (forward *Forward) handleForwardListener(mgr *manager.Manager, listener net
}

func (forward *Forward) handleForward(mgr *manager.Manager, conn net.Conn, route string, uuid string, seq uint64) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
// tell agent to start
startHeader := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
4 changes: 2 additions & 2 deletions admin/handler/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func AddMemo(taskChan chan *topology.TopoTask, info []string, uuid string, route
memo = memo + " " + i
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

topoTask := &topology.TopoTask{
Mode: topology.UPDATEMEMO,
Expand Down Expand Up @@ -51,7 +51,7 @@ func DelMemo(taskChan chan *topology.TopoTask, uuid string, route string) {
}
taskChan <- topoTask

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
4 changes: 2 additions & 2 deletions admin/handler/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (listen *Listen) LetListen(mgr *manager.Manager, route, uuid string) error
}
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -99,7 +99,7 @@ func dispatchChildUUID(mgr *manager.Manager, topo *topology.Topology, parentUUID
topoResult = <-topo.ResultChan
route := topoResult.Route

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
2 changes: 1 addition & 1 deletion admin/handler/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func LetShutdown(route string, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
2 changes: 1 addition & 1 deletion admin/handler/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func LetShellStart(route string, uuid string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
8 changes: 4 additions & 4 deletions admin/handler/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (socks *Socks) LetSocks(mgr *manager.Manager, route string, uuid string) er
return err
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -134,7 +134,7 @@ func (socks *Socks) handleSocksListener(mgr *manager.Manager, listener net.Liste
}

func (socks *Socks) handleSocks(mgr *manager.Manager, conn net.Conn, route string, uuid string, seq uint64) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down Expand Up @@ -278,7 +278,7 @@ func startUDPAss(mgr *manager.Manager, topo *topology.Topology, seq uint64) {
OK: 0,
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

defer func() {
if err != nil {
Expand Down Expand Up @@ -329,7 +329,7 @@ func startUDPAss(mgr *manager.Manager, topo *topology.Topology, seq uint64) {
}

func handleUDPAss(mgr *manager.Manager, listener *net.UDPConn, route string, uuid string, seq uint64) {
sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

dataHeader := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
2 changes: 1 addition & 1 deletion admin/handler/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (ssh *SSH) LetSSH(route string, uuid string) error {
}
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
2 changes: 1 addition & 1 deletion admin/handler/sshtunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (sshTunnel *SSHTunnel) LetSSHTunnel(route, uuid string) error {
}
}

sMessage := protocol.PrepareAndDecideWhichSProtoToLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: protocol.ADMIN_UUID,
Expand Down
10 changes: 5 additions & 5 deletions admin/initial/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func dispatchUUID(conn net.Conn, secret string) string {
Route: protocol.TEMP_ROUTE,
}

sMessage = protocol.PrepareAndDecideWhichSProtoToLower(conn, secret, protocol.ADMIN_UUID)
sMessage = protocol.NewDownMsg(conn, secret, protocol.ADMIN_UUID)

protocol.ConstructMessage(sMessage, header, uuidMess, false)
sMessage.SendMessage()
Expand Down Expand Up @@ -95,12 +95,12 @@ func NormalActive(userOptions *Options, topo *topology.Topology, proxy share.Pro
os.Exit(0)
}

sMessage = protocol.PrepareAndDecideWhichSProtoToLower(conn, userOptions.Secret, protocol.ADMIN_UUID)
sMessage = protocol.NewDownMsg(conn, userOptions.Secret, protocol.ADMIN_UUID)

protocol.ConstructMessage(sMessage, header, hiMess, false)
sMessage.SendMessage()

rMessage = protocol.PrepareAndDecideWhichRProtoFromLower(conn, userOptions.Secret, protocol.ADMIN_UUID)
rMessage = protocol.NewDownMsg(conn, userOptions.Secret, protocol.ADMIN_UUID)
fHeader, fMessage, err := protocol.DestructMessage(rMessage)

if err != nil {
Expand Down Expand Up @@ -213,7 +213,7 @@ func NormalPassive(userOptions *Options, topo *topology.Topology) net.Conn {
continue
}

rMessage = protocol.PrepareAndDecideWhichRProtoFromLower(conn, userOptions.Secret, protocol.ADMIN_UUID)
rMessage = protocol.NewDownMsg(conn, userOptions.Secret, protocol.ADMIN_UUID)
fHeader, fMessage, err := protocol.DestructMessage(rMessage)

if err != nil {
Expand All @@ -225,7 +225,7 @@ func NormalPassive(userOptions *Options, topo *topology.Topology) net.Conn {
if fHeader.MessageType == protocol.HI {
mmess := fMessage.(*protocol.HIMess)
if mmess.Greeting == "Shhh..." && mmess.IsAdmin == 0 {
sMessage = protocol.PrepareAndDecideWhichSProtoToLower(conn, userOptions.Secret, protocol.ADMIN_UUID)
sMessage = protocol.NewDownMsg(conn, userOptions.Secret, protocol.ADMIN_UUID)
protocol.ConstructMessage(sMessage, header, hiMess, false)
sMessage.SendMessage()

Expand Down
3 changes: 1 addition & 2 deletions admin/initial/parser.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package initial

Expand Down Expand Up @@ -49,7 +48,7 @@ func init() {
flag.StringVar(&args.Socks5ProxyP, "socks5-proxyp", "", "socks5 password")
flag.StringVar(&args.HttpProxy, "http-proxy", "", "The http proxy server ip:port you want to use")
flag.StringVar(&args.Downstream, "down", "raw", "Downstream data type you want to use")
flag.StringVar(&args.Domain, "domain", "", "Domain name for TLS SNI")
flag.StringVar(&args.Domain, "domain", "", "Domain name for TLS SNI/WS HOST")
flag.BoolVar(&args.TlsEnable, "tls-enable", false, "Encrypt connection by TLS")

flag.Usage = newUsage
Expand Down
3 changes: 1 addition & 2 deletions admin/initial/parser_win.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build windows
// +build windows

package initial

Expand Down Expand Up @@ -47,7 +46,7 @@ func init() {
flag.StringVar(&args.Socks5ProxyP, "socks5-proxyp", "", "socks5 password")
flag.StringVar(&args.HttpProxy, "http-proxy", "", "The http proxy server ip:port you want to use")
flag.StringVar(&args.Downstream, "down", "raw", "")
flag.StringVar(&args.Domain, "domain", "", "Domain name for TLS SNI")
flag.StringVar(&args.Domain, "domain", "", "Domain name for TLS SNI/WS HOST")
flag.BoolVar(&args.TlsEnable, "tls-enable", false, "Encrypt connection by TLS")

flag.Usage = newUsage
Expand Down
4 changes: 2 additions & 2 deletions admin/process/process.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
//go:build !windows

package process

Expand Down Expand Up @@ -51,7 +51,7 @@ func (admin *Admin) Run() {
}

func (admin *Admin) handleMessFromDownstream(console *cli.Console) {
rMessage := protocol.PrepareAndDecideWhichRProtoFromLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
rMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

for {
header, message, err := protocol.DestructMessage(rMessage)
Expand Down
4 changes: 2 additions & 2 deletions admin/process/process_win.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build windows
//go:build windows

package process

Expand Down Expand Up @@ -49,7 +49,7 @@ func (admin *Admin) Run() {
}

func (admin *Admin) handleMessFromDownstream(console *cli.Console) {
rMessage := protocol.PrepareAndDecideWhichRProtoFromLower(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
rMessage := protocol.NewDownMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

for {
header, message, err := protocol.DestructMessage(rMessage)
Expand Down
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {

agent := process.NewAgent(options)

protocol.DecideType(options.Upstream, options.Downstream)
protocol.SetUpDownStream(options.Upstream, options.Downstream)

var conn net.Conn
switch options.Mode {
Expand Down
10 changes: 5 additions & 5 deletions agent/handler/backward.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (backward *Backward) start(mgr *manager.Manager) {
mgr.BackwardManager.TaskChan <- mgrTask
<-mgr.BackwardManager.ResultChan

sMessage := protocol.PrepareAndDecideWhichSProtoToUpper(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewUpMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

for {
conn, err := backward.Listener.Accept()
Expand Down Expand Up @@ -84,7 +84,7 @@ func (backward *Backward) start(mgr *manager.Manager) {
}

func (backward *Backward) handleBackward(mgr *manager.Manager, conn net.Conn, seq uint64) {
sMessage := protocol.PrepareAndDecideWhichSProtoToUpper(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewUpMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

defer func() {
finHeader := &protocol.Header{
Expand Down Expand Up @@ -170,7 +170,7 @@ func (backward *Backward) handleBackward(mgr *manager.Manager, conn net.Conn, se
}

func testBackward(mgr *manager.Manager, lPort, rPort string) {
sMessage := protocol.PrepareAndDecideWhichSProtoToUpper(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewUpMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: global.G_Component.UUID,
Expand Down Expand Up @@ -207,7 +207,7 @@ func testBackward(mgr *manager.Manager, lPort, rPort string) {
func sendDoneMess(all uint16, rPort string) {
// here is a problem,if some of the backward conns cannot send FIN before DONE,then the FIN they send cannot be processed by admin
// but it's not a really big problem,because users must know some data maybe lost since they choose to close backward
sMessage := protocol.PrepareAndDecideWhichSProtoToUpper(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewUpMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

header := &protocol.Header{
Sender: global.G_Component.UUID,
Expand Down Expand Up @@ -249,7 +249,7 @@ func DispatchBackwardMess(mgr *manager.Manager) {
result.SeqChan <- mess.Seq
<-mgr.BackwardManager.SeqReady
} else {
sMessage := protocol.PrepareAndDecideWhichSProtoToUpper(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)
sMessage := protocol.NewUpMsg(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID)

finHeader := &protocol.Header{
Sender: global.G_Component.UUID,
Expand Down
Loading

0 comments on commit e48c50b

Please sign in to comment.