From 900ca633727bb0916ab5926dc7460edefd335ebd Mon Sep 17 00:00:00 2001 From: ph4ntonn Date: Wed, 22 Apr 2020 23:15:38 +0800 Subject: [PATCH] Sort through codes structure --- admin/admin.go | 83 ++++++------- admin/command.go | 6 +- admin/function.go | 48 ++++---- admin/interactive.go | 27 ++-- admin/topology.go | 28 ++--- agent/agent.go | 177 ++++++++++++++------------- agent/command.go | 6 +- agent/forward.go | 20 +-- agent/function.go | 24 ++-- agent/reflect.go | 22 ++-- agent/shell.go | 6 +- agent/socks.go | 19 ++- socks/socks.go => agent/socksfunc.go | 22 ++-- agent/ssh.go | 12 +- agent/sshtunnel.go | 26 ++-- node/init.go | 50 ++++---- node/iptables_reuse.go | 20 +-- node/reuse.go | 20 +-- node/soreuse_reuse.go | 20 +-- {common => share}/file.go | 31 ++--- {common => share}/heartbeat.go | 5 +- {common => utils}/payload.go | 2 +- {common => utils}/utils.go | 2 +- 23 files changed, 340 insertions(+), 336 deletions(-) rename socks/socks.go => agent/socksfunc.go (69%) rename {common => share}/file.go (68%) rename {common => share}/heartbeat.go (55%) rename {common => utils}/payload.go (99%) rename {common => utils}/utils.go (99%) diff --git a/admin/admin.go b/admin/admin.go index 5e81518..8a7eb01 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -8,26 +8,27 @@ import ( "strconv" "strings" - "Stowaway/common" "Stowaway/node" + "Stowaway/share" + "Stowaway/utils" ) var ( CliStatus *string CurrentClient []string //记录当前网络中的节点,主要用来将string型的id对照至int型的序号,方便用户理解 - AdminStatus *common.AdminStatus - FileDataMap *common.IntStrMap - ClientSockets *common.Uint32ConnMap - PortForWardMap *common.Uint32ConnMap + AdminStatus *utils.AdminStatus + FileDataMap *utils.IntStrMap + ClientSockets *utils.Uint32ConnMap + PortForWardMap *utils.Uint32ConnMap ) //启动admin -func NewAdmin(c *common.AdminOptions) { +func NewAdmin(c *utils.AdminOptions) { var InitStatus string = "admin" - AdminStatus = common.NewAdminStatus() - ClientSockets = common.NewUint32ConnMap() - FileDataMap = common.NewIntStrMap() - PortForWardMap = common.NewUint32ConnMap() + AdminStatus = utils.NewAdminStatus() + ClientSockets = utils.NewUint32ConnMap() + FileDataMap = utils.NewIntStrMap() + PortForWardMap = utils.NewUint32ConnMap() AdminStatus.AESKey = []byte(c.Secret) listenPort := c.Listen startnodeaddr := c.Connect @@ -72,19 +73,19 @@ func ConnectToStartNode(startnodeaddr string, rhostreuse bool) { } } - helloMess, _ := common.ConstructPayload(common.StartNodeId, "", "COMMAND", "STOWAWAYADMIN", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + helloMess, _ := utils.ConstructPayload(utils.StartNodeId, "", "COMMAND", "STOWAWAYADMIN", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(helloMess) for { - command, _ := common.ExtractPayload(startNodeConn, AdminStatus.AESKey, common.AdminId, true) + command, _ := utils.ExtractPayload(startNodeConn, AdminStatus.AESKey, utils.AdminId, true) switch command.Command { case "INIT": - respCommand, _ := common.ConstructPayload(common.StartNodeId, "", "COMMAND", "ID", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.StartNodeId, "", "COMMAND", "ID", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respCommand) AdminStuff.StartNode = strings.Split(startNodeConn.RemoteAddr().String(), ":")[0] log.Printf("[*]Connect to startnode %s successfully!\n", startNodeConn.RemoteAddr().String()) - NodeStatus.Nodenote[common.StartNodeId] = "" - CurrentClient = append(CurrentClient, common.StartNodeId) //记录startnode加入网络 - AddNodeToTopology(common.StartNodeId, common.AdminId) + NodeStatus.Nodenote[utils.StartNodeId] = "" + CurrentClient = append(CurrentClient, utils.StartNodeId) //记录startnode加入网络 + AddNodeToTopology(utils.StartNodeId, utils.AdminId) CalRoute() go HandleStartConn(startNodeConn) go HandleCommandToControlConn(startNodeConn) @@ -120,21 +121,21 @@ func StartListen(listenPort string) { // 初始化与startnode的连接 func HandleInitControlConn(startNodeConn net.Conn) { for { - command, err := common.ExtractPayload(startNodeConn, AdminStatus.AESKey, common.AdminId, true) + command, err := utils.ExtractPayload(startNodeConn, AdminStatus.AESKey, utils.AdminId, true) if err != nil { log.Println("[*]Startnode seems offline, control channel set up failed.Exiting...") return } switch command.Command { case "STOWAWAYAGENT": - Message, _ := common.ConstructPayload(common.StartNodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + Message, _ := utils.ConstructPayload(utils.StartNodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(Message) case "INIT": - respCommand, _ := common.ConstructPayload(common.StartNodeId, "", "COMMAND", "ID", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.StartNodeId, "", "COMMAND", "ID", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respCommand) - NodeStatus.Nodenote[common.StartNodeId] = "" - CurrentClient = append(CurrentClient, common.StartNodeId) //记录startnode加入网络 - AddNodeToTopology(common.StartNodeId, common.AdminId) + NodeStatus.Nodenote[utils.StartNodeId] = "" + CurrentClient = append(CurrentClient, utils.StartNodeId) //记录startnode加入网络 + AddNodeToTopology(utils.StartNodeId, utils.AdminId) CalRoute() go HandleStartConn(startNodeConn) go HandleCommandToControlConn(startNodeConn) @@ -146,10 +147,10 @@ func HandleInitControlConn(startNodeConn net.Conn) { // 处理与startnode的信道 func HandleStartConn(startNodeConn net.Conn) { for { - nodeResp, err := common.ExtractPayload(startNodeConn, AdminStatus.AESKey, common.AdminId, true) + nodeResp, err := utils.ExtractPayload(startNodeConn, AdminStatus.AESKey, utils.AdminId, true) if err != nil { log.Println("[*]StartNode seems offline") - DelNodeFromTopology(common.StartNodeId) + DelNodeFromTopology(utils.StartNodeId) AdminStuff.StartNode = "offline" startNodeConn.Close() break @@ -184,7 +185,7 @@ func HandleStartConn(startNodeConn net.Conn) { } ClientSockets.Unlock() Route.Lock() - respCommand, _ := common.ConstructPayload(nodeResp.CurrentId, Route.Route[nodeResp.CurrentId], "DATA", "FINOK", " ", " ", nodeResp.Clientid, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeResp.CurrentId, Route.Route[nodeResp.CurrentId], "DATA", "FINOK", " ", " ", nodeResp.Clientid, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(respCommand) case "FILEDATA": //接收文件内容 @@ -218,7 +219,7 @@ func HandleStartConn(startNodeConn net.Conn) { ReflectConnMap.Unlock() PortReflectMap.Lock() if _, ok := PortReflectMap.Payload[nodeResp.Clientid]; ok { - if !common.IsClosed(PortReflectMap.Payload[nodeResp.Clientid]) { + if !utils.IsClosed(PortReflectMap.Payload[nodeResp.Clientid]) { close(PortReflectMap.Payload[nodeResp.Clientid]) delete(PortReflectMap.Payload, nodeResp.Clientid) } @@ -250,7 +251,7 @@ func HandleStartConn(startNodeConn net.Conn) { AddNodeToTopology(nodeid, nodeResp.CurrentId) //加入拓扑 CalRoute() //计算路由 Route.Lock() - respCommand, _ := common.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "ID", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "ID", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(respCommand) case "AGENTOFFLINE": @@ -309,45 +310,45 @@ func HandleStartConn(startNodeConn net.Conn) { UploadFile, err := os.Create(nodeResp.Info) Route.Lock() if err != nil { - respComm, _ := common.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "CREATEFAIL", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "CREATEFAIL", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respComm) } else { - respComm, _ := common.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "NAMECONFIRM", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "NAMECONFIRM", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respComm) - go common.ReceiveFile(Route.Route[CurrentNode], &startNodeConn, FileDataMap, AdminStatus.CannotRead, UploadFile, AdminStatus.AESKey, true, common.AdminId) + go share.ReceiveFile(Route.Route[CurrentNode], &startNodeConn, FileDataMap, AdminStatus.CannotRead, UploadFile, AdminStatus.AESKey, true, utils.AdminId) } Route.Unlock() case "FILESIZE": - common.File.FileSize, _ = strconv.ParseInt(nodeResp.Info, 10, 64) + share.File.FileSize, _ = strconv.ParseInt(nodeResp.Info, 10, 64) Route.Lock() - respComm, _ := common.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "FILESIZECONFIRM", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "FILESIZECONFIRM", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(respComm) - common.File.ReceiveFileSize <- true + share.File.ReceiveFileSize <- true case "FILESLICENUM": - common.File.TotalSilceNum, _ = strconv.Atoi(nodeResp.Info) + share.File.TotalSilceNum, _ = strconv.Atoi(nodeResp.Info) Route.Lock() - respComm, _ := common.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "FILESLICENUMCONFIRM", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "FILESLICENUMCONFIRM", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(respComm) - common.File.ReceiveFileSliceNum <- true + share.File.ReceiveFileSliceNum <- true case "FILESLICENUMCONFIRM": - common.File.TotalConfirm <- true + share.File.TotalConfirm <- true case "FILESIZECONFIRM": - common.File.TotalConfirm <- true + share.File.TotalConfirm <- true case "FILENOTEXIST": fmt.Printf("[*]File %s not exist!\n", nodeResp.Info) case "CANNOTREAD": fmt.Printf("[*]File %s cannot be read!\n", nodeResp.Info) AdminStatus.CannotRead <- true - common.File.ReceiveFileSliceNum <- false + share.File.ReceiveFileSliceNum <- false os.Remove(nodeResp.Info) case "CANNOTUPLOAD": fmt.Printf("[*]Agent cannot read file: %s\n", nodeResp.Info) case "GETREFLECTNUM": Route.Lock() AdminStuff.Lock() - respComm, _ := common.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "REFLECTNUM", " ", " ", AdminStuff.ReflectNum, common.AdminId, AdminStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(CurrentNode, Route.Route[CurrentNode], "COMMAND", "REFLECTNUM", " ", " ", AdminStuff.ReflectNum, utils.AdminId, AdminStatus.AESKey, false) AdminStuff.ReflectNum++ AdminStuff.Unlock() Route.Unlock() @@ -361,7 +362,7 @@ func HandleStartConn(startNodeConn net.Conn) { AddNodeToTopology(nodeResp.CurrentId, uppernode) CalRoute() case "HEARTBEAT": - hbcommpack, _ := common.ConstructPayload(common.StartNodeId, "", "COMMAND", "KEEPALIVE", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + hbcommpack, _ := utils.ConstructPayload(utils.StartNodeId, "", "COMMAND", "KEEPALIVE", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(hbcommpack) case "TRANSSUCCESS": fmt.Println("[*]File transmission complete!") diff --git a/admin/command.go b/admin/command.go index c0f2bac..8ce89d8 100644 --- a/admin/command.go +++ b/admin/command.go @@ -1,16 +1,16 @@ package admin import ( - "Stowaway/common" + "Stowaway/utils" "flag" "log" "os" ) -var Args *common.AdminOptions +var Args *utils.AdminOptions func init() { - Args = new(common.AdminOptions) + Args = new(utils.AdminOptions) flag.StringVar(&Args.Secret, "s", "", "Communication secret") flag.StringVar(&Args.Listen, "l", "", "Listen port") flag.StringVar(&Args.Connect, "c", "", "The startnode address when you actively connect to it") diff --git a/admin/function.go b/admin/function.go index 3ce5ae9..4b92574 100644 --- a/admin/function.go +++ b/admin/function.go @@ -1,7 +1,7 @@ package admin import ( - "Stowaway/common" + "Stowaway/utils" "bufio" "fmt" "io/ioutil" @@ -14,20 +14,20 @@ import ( ) var ( - AdminStuff *common.AdminStuff - NodeStatus *common.NodeStatus - ForwardStatus *common.ForwardStatus - ReflectConnMap *common.Uint32ConnMap - PortReflectMap *common.Uint32ChanStrMap + AdminStuff *utils.AdminStuff + NodeStatus *utils.NodeStatus + ForwardStatus *utils.ForwardStatus + ReflectConnMap *utils.Uint32ConnMap + PortReflectMap *utils.Uint32ChanStrMap ) var WaitForFindAll chan bool func init() { - ReflectConnMap = common.NewUint32ConnMap() - PortReflectMap = common.NewUint32ChanStrMap() - NodeStatus = common.NewNodeStatus() - ForwardStatus = common.NewForwardStatus() - AdminStuff = common.NewAdminStuff() + ReflectConnMap = utils.NewUint32ConnMap() + PortReflectMap = utils.NewUint32ChanStrMap() + NodeStatus = utils.NewNodeStatus() + ForwardStatus = utils.NewForwardStatus() + AdminStuff = utils.NewAdminStuff() WaitForFindAll = make(chan bool, 1) } @@ -75,7 +75,7 @@ func StartSocksServiceForClient(command []string, startNodeConn net.Conn, nodeID socks5Addr := fmt.Sprintf("0.0.0.0:%s", socksPort) socksListenerForClient, err := net.Listen("tcp", socks5Addr) if err != nil { - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "SOCKSOFF", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "SOCKSOFF", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) _, err = startNodeConn.Write(respCommand) if err != nil { log.Println("[*]Cannot stop agent's socks service,check the connection!") @@ -114,11 +114,11 @@ func HandleNewSocksConn(startNodeConn net.Conn, clientsocks net.Conn, num uint32 len, err := clientsocks.Read(buffer) if err != nil { clientsocks.Close() - finMessage, _ := common.ConstructPayload(nodeID, route, "DATA", "FIN", " ", " ", num, common.AdminId, AdminStatus.AESKey, false) + finMessage, _ := utils.ConstructPayload(nodeID, route, "DATA", "FIN", " ", " ", num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(finMessage) return } else { - respData, _ := common.ConstructPayload(nodeID, route, "DATA", "SOCKSDATA", " ", string(buffer[:len]), num, common.AdminId, AdminStatus.AESKey, false) + respData, _ := utils.ConstructPayload(nodeID, route, "DATA", "SOCKSDATA", " ", string(buffer[:len]), num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respData) } } @@ -154,7 +154,7 @@ func StartSSHService(startNodeConn net.Conn, info []string, nodeid string, metho information := fmt.Sprintf("%s:::%s:::%s:::%s", info[0], info[1], info[2], method) Route.Lock() - sshCommand, _ := common.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "SSH", " ", information, 0, common.AdminId, AdminStatus.AESKey, false) + sshCommand, _ := utils.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "SSH", " ", information, 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(sshCommand) @@ -175,7 +175,7 @@ func SendSSHTunnel(startNodeConn net.Conn, info []string, nodeid string, method information := fmt.Sprintf("%s:::%s:::%s:::%s:::%s", info[0], info[1], info[2], info[3], method) Route.Lock() - sshCommand, _ := common.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "SSHTUNNEL", " ", information, 0, common.AdminId, AdminStatus.AESKey, false) + sshCommand, _ := utils.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "SSHTUNNEL", " ", information, 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(sshCommand) @@ -188,18 +188,18 @@ func HandleForwardPort(forwardconn net.Conn, target string, startNodeConn net.Co route := Route.Route[nodeid] Route.Unlock() - forwardCommand, _ := common.ConstructPayload(nodeid, route, "DATA", "FORWARD", " ", target, num, common.AdminId, AdminStatus.AESKey, false) + forwardCommand, _ := utils.ConstructPayload(nodeid, route, "DATA", "FORWARD", " ", target, num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(forwardCommand) buffer := make([]byte, 10240) for { len, err := forwardconn.Read(buffer) if err != nil { - finMessage, _ := common.ConstructPayload(nodeid, route, "DATA", "FORWARDFIN", " ", " ", num, common.AdminId, AdminStatus.AESKey, false) + finMessage, _ := utils.ConstructPayload(nodeid, route, "DATA", "FORWARDFIN", " ", " ", num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(finMessage) return } else { - respData, _ := common.ConstructPayload(nodeid, route, "DATA", "FORWARDDATA", " ", string(buffer[:len]), num, common.AdminId, AdminStatus.AESKey, false) + respData, _ := utils.ConstructPayload(nodeid, route, "DATA", "FORWARDDATA", " ", string(buffer[:len]), num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respData) } } @@ -283,7 +283,7 @@ func TryReflect(startNodeConn net.Conn, nodeid string, id uint32, port string) { ReflectConnMap.Unlock() } else { Route.Lock() - respdata, _ := common.ConstructPayload(nodeid, Route.Route[nodeid], "DATA", "REFLECTTIMEOUT", " ", " ", id, common.AdminId, AdminStatus.AESKey, false) + respdata, _ := utils.ConstructPayload(nodeid, Route.Route[nodeid], "DATA", "REFLECTTIMEOUT", " ", " ", id, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(respdata) return @@ -315,11 +315,11 @@ func HandleReflect(startNodeConn net.Conn, reflectDataChan chan string, num uint for { len, err := reflectConn.Read(serverBuffer) if err != nil { - respData, _ := common.ConstructPayload(nodeid, route, "DATA", "REFLECTOFFLINE", " ", " ", num, common.AdminId, AdminStatus.AESKey, false) + respData, _ := utils.ConstructPayload(nodeid, route, "DATA", "REFLECTOFFLINE", " ", " ", num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respData) return } - respData, _ := common.ConstructPayload(nodeid, route, "DATA", "REFLECTDATARESP", " ", string(serverBuffer[:len]), num, common.AdminId, AdminStatus.AESKey, false) + respData, _ := utils.ConstructPayload(nodeid, route, "DATA", "REFLECTDATARESP", " ", string(serverBuffer[:len]), num, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respData) } }() @@ -329,7 +329,7 @@ func StopReflect(startNodeConn net.Conn, nodeid string) { fmt.Println("[*]Stop command has been sent") Route.Lock() - command, _ := common.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "STOPREFLECT", " ", " ", 0, common.AdminId, AdminStatus.AESKey, false) + command, _ := utils.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", "STOPREFLECT", " ", " ", 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(command) @@ -339,7 +339,7 @@ func StopReflect(startNodeConn net.Conn, nodeid string) { //测试是否端口可用 func TestIfValid(commandtype string, startNodeConn net.Conn, target string, nodeid string) { Route.Lock() - command, _ := common.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", commandtype, " ", target, 0, common.AdminId, AdminStatus.AESKey, false) + command, _ := utils.ConstructPayload(nodeid, Route.Route[nodeid], "COMMAND", commandtype, " ", target, 0, utils.AdminId, AdminStatus.AESKey, false) Route.Unlock() startNodeConn.Write(command) diff --git a/admin/interactive.go b/admin/interactive.go index df34872..add304c 100644 --- a/admin/interactive.go +++ b/admin/interactive.go @@ -1,7 +1,8 @@ package admin import ( - "Stowaway/common" + "Stowaway/share" + "Stowaway/utils" "bufio" "fmt" "log" @@ -27,7 +28,7 @@ func HandleNodeCommand(startNodeConn net.Conn, nodeID string) { AdminCommand := <-AdminStuff.AdminCommandChan switch AdminCommand[0] { case "shell": - respCommand, err := common.ConstructPayload(nodeID, route, "COMMAND", "SHELL", " ", "", 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, err := utils.ConstructPayload(nodeID, route, "COMMAND", "SHELL", " ", "", 0, utils.AdminId, AdminStatus.AESKey, false) _, err = startNodeConn.Write(respCommand) if err != nil { log.Printf("[*]ERROR OCCURED!: %s", err) @@ -50,7 +51,7 @@ func HandleNodeCommand(startNodeConn net.Conn, nodeID string) { AdminStatus.IsShellMode <- true continue } - respCommand, err := common.ConstructPayload(nodeID, route, "COMMAND", "SOCKS", " ", socksStartData, 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, err := utils.ConstructPayload(nodeID, route, "COMMAND", "SOCKS", " ", socksStartData, 0, utils.AdminId, AdminStatus.AESKey, false) _, err = startNodeConn.Write(respCommand) if err != nil { log.Println("[*]StartNode seems offline") @@ -189,7 +190,7 @@ func HandleNodeCommand(startNodeConn net.Conn, nodeID string) { continue } data := AdminCommand[1] + ":::" + choice - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "CONNECT", " ", data, 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "CONNECT", " ", data, 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respCommand) break } @@ -207,7 +208,7 @@ func HandleNodeCommand(startNodeConn net.Conn, nodeID string) { AdminStatus.IsShellMode <- true continue } - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "LISTEN", " ", AdminCommand[1], 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "LISTEN", " ", AdminCommand[1], 0, utils.AdminId, AdminStatus.AESKey, false) startNodeConn.Write(respCommand) } else { fmt.Println("[*]Bad format! Should be listen [port]") @@ -216,7 +217,7 @@ func HandleNodeCommand(startNodeConn net.Conn, nodeID string) { AdminStatus.IsShellMode <- true case "upload": if len(AdminCommand) == 2 { - go common.UploadFile(route, AdminCommand[1], &startNodeConn, nodeID, AdminStatus.GetName, AdminStatus.AESKey, common.AdminId, true) + go share.UploadFile(route, AdminCommand[1], &startNodeConn, nodeID, AdminStatus.GetName, AdminStatus.AESKey, utils.AdminId, true) } else { fmt.Println("[*]Bad format! Should be upload [filename]") } @@ -224,7 +225,7 @@ func HandleNodeCommand(startNodeConn net.Conn, nodeID string) { AdminStatus.IsShellMode <- true case "download": if len(AdminCommand) == 2 { - go common.DownloadFile(route, AdminCommand[1], startNodeConn, nodeID, common.AdminId, AdminStatus.AESKey) + go share.DownloadFile(route, AdminCommand[1], startNodeConn, nodeID, utils.AdminId, AdminStatus.AESKey) } else { fmt.Println("[*]Bad format! Should be download [filename]") } @@ -312,18 +313,18 @@ func HandleShellToNode(startNodeControlConn net.Conn, nodeID string) { } switch command { case "exit\n": - if nodeID == common.StartNodeId { + if nodeID == utils.StartNodeId { *CliStatus = "startnode" } else { *CliStatus = "node " + fmt.Sprint(FindIntByNodeid(nodeID)+1) } - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "SHELL", " ", command, 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "SHELL", " ", command, 0, utils.AdminId, AdminStatus.AESKey, false) startNodeControlConn.Write(respCommand) AdminStatus.ReadyChange <- true AdminStatus.IsShellMode <- true return default: - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "SHELL", " ", command, 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "SHELL", " ", command, 0, utils.AdminId, AdminStatus.AESKey, false) startNodeControlConn.Write(respCommand) } } @@ -348,12 +349,12 @@ func HandleSSHToNode(startNodeControlConn net.Conn, nodeID string) { } switch command { case "exit\n": - if nodeID == common.StartNodeId { + if nodeID == utils.StartNodeId { *CliStatus = "startnode" } else { *CliStatus = "node " + fmt.Sprint(FindIntByNodeid(nodeID)+1) } - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "SSHCOMMAND", " ", command, 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "SSHCOMMAND", " ", command, 0, utils.AdminId, AdminStatus.AESKey, false) startNodeControlConn.Write(respCommand) AdminStatus.ReadyChange <- true AdminStatus.IsShellMode <- true @@ -361,7 +362,7 @@ func HandleSSHToNode(startNodeControlConn net.Conn, nodeID string) { case "\n": fmt.Print("(ssh mode)>>>") default: - respCommand, _ := common.ConstructPayload(nodeID, route, "COMMAND", "SSHCOMMAND", " ", command, 0, common.AdminId, AdminStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(nodeID, route, "COMMAND", "SSHCOMMAND", " ", command, 0, utils.AdminId, AdminStatus.AESKey, false) startNodeControlConn.Write(respCommand) } } diff --git a/admin/topology.go b/admin/topology.go index 692c2fe..8380b22 100644 --- a/admin/topology.go +++ b/admin/topology.go @@ -1,7 +1,7 @@ package admin import ( - "Stowaway/common" + "Stowaway/utils" "errors" "fmt" "strings" @@ -9,13 +9,13 @@ import ( "github.com/gofrs/uuid" ) -var Nooode *common.SafeNodeMap -var Route *common.SafeRouteMap +var Nooode *utils.SafeNodeMap +var Route *utils.SafeRouteMap var readyToDel []string func init() { - Nooode = common.NewSafeNodeMap() - Route = common.NewSafeRouteMap() + Nooode = utils.NewSafeNodeMap() + Route = utils.NewSafeRouteMap() } /*-------------------------节点拓扑相关代码--------------------------*/ @@ -25,11 +25,11 @@ func AddNodeToTopology(nodeid string, uppernodeid string) { if _, ok := Nooode.AllNode[nodeid]; ok { Nooode.AllNode[nodeid].Uppernode = uppernodeid } else { - tempnode := common.NewNode() + tempnode := utils.NewNode() Nooode.AllNode[nodeid] = tempnode Nooode.AllNode[nodeid].Uppernode = uppernodeid } - if uppernodeid != common.AdminId { + if uppernodeid != utils.AdminId { Nooode.AllNode[uppernodeid].Lowernode = append(Nooode.AllNode[uppernodeid].Lowernode, nodeid) } Nooode.Unlock() @@ -61,7 +61,7 @@ func DelNodeFromTopology(nodeid string) { if _, ok := Nooode.AllNode[nodeid]; ok { uppernode := Nooode.AllNode[nodeid].Uppernode if _, ok := Nooode.AllNode[uppernode]; ok { - index := common.FindSpecFromSlice(nodeid, Nooode.AllNode[uppernode].Lowernode) + index := utils.FindSpecFromSlice(nodeid, Nooode.AllNode[uppernode].Lowernode) Nooode.AllNode[uppernode].Lowernode = append(Nooode.AllNode[uppernode].Lowernode[:index], Nooode.AllNode[uppernode].Lowernode[index+1:]...) } @@ -114,17 +114,17 @@ func CalRoute() { var temp []string = []string{} count := key - if key == common.AdminId { + if key == utils.AdminId { continue } for { - if Nooode.AllNode[count].Uppernode != common.AdminId && Nooode.AllNode[count].Uppernode != common.StartNodeId { + if Nooode.AllNode[count].Uppernode != utils.AdminId && Nooode.AllNode[count].Uppernode != utils.StartNodeId { count = Nooode.AllNode[count].Uppernode node := count temp = append(temp, node) } else { - common.StringReverse(temp) + utils.StringReverse(temp) route := strings.Join(temp, ":") Route.Lock() Route.Route[key] = route @@ -142,7 +142,7 @@ func ShowDetail() { if AdminStuff.StartNode != "0.0.0.0" { var nodes []string - fmt.Printf("StartNode:[1] %s note:%s\n", AdminStuff.StartNode, NodeStatus.Nodenote[common.StartNodeId]) + fmt.Printf("StartNode:[1] %s note:%s\n", AdminStuff.StartNode, NodeStatus.Nodenote[utils.StartNodeId]) for Nodeid, _ := range NodeStatus.Nodes { nodes = append(nodes, Nodeid) @@ -175,7 +175,7 @@ func ShowTree() { for _, value := range nodesid { node := CurrentClient[value] nodestatus := Nooode.AllNode[node] - if node == common.StartNodeId { + if node == utils.StartNodeId { fmt.Printf("StartNode[%s]'s child nodes:\n", fmt.Sprint(value+1)) if len(nodestatus.Lowernode) == 0 { fmt.Println("\tThere is no child node for this one.") @@ -264,7 +264,7 @@ func FindNumByNodeid(id string) (string, error) { return "", NO_NODE } - nodeid := common.StrUint32(id) + nodeid := utils.StrUint32(id) currentid := int(nodeid) - 1 if len(CurrentClient) < int(nodeid) { diff --git a/agent/agent.go b/agent/agent.go index 3bbff0a..cc84f56 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -1,8 +1,9 @@ package agent import ( - "Stowaway/common" "Stowaway/node" + "Stowaway/share" + "Stowaway/utils" "fmt" "io" "net" @@ -12,20 +13,20 @@ import ( ) var ( - ProxyChan *common.ProxyChan - SocksInfo *common.SocksSetting - AgentStatus *common.AgentStatus - FileDataMap *common.IntStrMap - SocksDataChanMap *common.Uint32ChanStrMap + ProxyChan *utils.ProxyChan + SocksInfo *utils.SocksSetting + AgentStatus *utils.AgentStatus + FileDataMap *utils.IntStrMap + SocksDataChanMap *utils.Uint32ChanStrMap ) var ConnToAdmin net.Conn -func NewAgent(c *common.AgentOptions) { - AgentStatus = common.NewAgentStatus() - SocksInfo = common.NewSocksSetting() - ProxyChan = common.NewProxyChan() - SocksDataChanMap = common.NewUint32ChanStrMap() - FileDataMap = common.NewIntStrMap() +func NewAgent(c *utils.AgentOptions) { + AgentStatus = utils.NewAgentStatus() + SocksInfo = utils.NewSocksSetting() + ProxyChan = utils.NewProxyChan() + SocksDataChanMap = utils.NewUint32ChanStrMap() + FileDataMap = utils.NewIntStrMap() AgentStatus.AESKey = []byte(c.Secret) listenPort := c.Listen @@ -67,12 +68,12 @@ func NewAgent(c *common.AgentOptions) { // 后续想让startnode与simplenode实现不一样的功能,故将两种node实现代码分开写 func StartNodeInit(monitor, listenPort, reConn string, passive bool) { var err error - AgentStatus.Nodeid = common.StartNodeId + AgentStatus.Nodeid = utils.StartNodeId ConnToAdmin, AgentStatus.Nodeid, err = node.StartNodeConn(monitor, listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) if err != nil { os.Exit(0) } - go common.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) + go share.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) go HandleStartNodeConn(&ConnToAdmin, monitor, listenPort, reConn, passive, AgentStatus.Nodeid) go node.StartNodeListen(listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) go PrepareForReOnlineNode() @@ -82,7 +83,7 @@ func StartNodeInit(monitor, listenPort, reConn string, passive bool) { <-node.NodeStuff.IsAdmin //正常模式启动的节点被连接一定是agent来连接,所以这里不需要判断是否是admin连接 ProxyChan.ProxyChanToUpperNode <- NewNodeMessage if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -94,7 +95,7 @@ func StartNodeInit(monitor, listenPort, reConn string, passive bool) { //普通的node节点 func SimpleNodeInit(monitor, listenPort string, rhostreuse bool) { var err error - AgentStatus.Nodeid = common.AdminId + AgentStatus.Nodeid = utils.AdminId if !rhostreuse { //连接的节点是否是在reuseport? ConnToAdmin, AgentStatus.Nodeid, err = node.StartNodeConn(monitor, listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) } else { @@ -103,7 +104,7 @@ func SimpleNodeInit(monitor, listenPort string, rhostreuse bool) { if err != nil { os.Exit(0) } - go common.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) + go share.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) go HandleSimpleNodeConn(&ConnToAdmin, AgentStatus.Nodeid) go node.StartNodeListen(listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) go PrepareForReOnlineNode() @@ -113,7 +114,7 @@ func SimpleNodeInit(monitor, listenPort string, rhostreuse bool) { <-node.NodeStuff.IsAdmin //正常模式启动的节点被连接一定是agent来连接,所以这里不需要判断是否是admin连接 ProxyChan.ProxyChanToUpperNode <- NewNodeMessage if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -124,9 +125,9 @@ func SimpleNodeInit(monitor, listenPort string, rhostreuse bool) { //reverse mode下的startnode节点 func StartNodeReversemodeInit(monitor, listenPort string, passive bool) { - AgentStatus.Nodeid = common.StartNodeId + AgentStatus.Nodeid = utils.StartNodeId ConnToAdmin, AgentStatus.Nodeid = node.AcceptConnFromUpperNode(listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) - go common.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) + go share.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) go HandleStartNodeConn(&ConnToAdmin, monitor, listenPort, "", passive, AgentStatus.Nodeid) go node.StartNodeListen(listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) go PrepareForReOnlineNode() @@ -140,7 +141,7 @@ func StartNodeReversemodeInit(monitor, listenPort string, passive bool) { } else { ProxyChan.ProxyChanToUpperNode <- NewNodeMessage if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -152,9 +153,9 @@ func StartNodeReversemodeInit(monitor, listenPort string, passive bool) { //reverse mode下的普通节点 func SimpleNodeReversemodeInit(monitor, listenPort string) { - AgentStatus.Nodeid = common.AdminId + AgentStatus.Nodeid = utils.AdminId ConnToAdmin, AgentStatus.Nodeid = node.AcceptConnFromUpperNode(listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) - go common.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) + go share.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) go HandleSimpleNodeConn(&ConnToAdmin, AgentStatus.Nodeid) go node.StartNodeListen(listenPort, AgentStatus.Nodeid, AgentStatus.AESKey) go PrepareForReOnlineNode() @@ -164,7 +165,7 @@ func SimpleNodeReversemodeInit(monitor, listenPort string) { <-node.NodeStuff.IsAdmin //被动模式启动的节点被连接一定是agent来连接,所以这里不需要判断是否是admin连接 ProxyChan.ProxyChanToUpperNode <- NewNodeMessage if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -175,7 +176,7 @@ func SimpleNodeReversemodeInit(monitor, listenPort string) { //reuseport下的startnode节点 func StartNodeReuseInit(reusehost, reuseport, localport string, method int) { - AgentStatus.Nodeid = common.StartNodeId + AgentStatus.Nodeid = utils.StartNodeId if method == 1 { ConnToAdmin, AgentStatus.Nodeid = node.AcceptConnFromUpperNodeReuse(reusehost, reuseport, AgentStatus.Nodeid, AgentStatus.AESKey) } else { @@ -186,7 +187,7 @@ func StartNodeReuseInit(reusehost, reuseport, localport string, method int) { } ConnToAdmin, AgentStatus.Nodeid = node.AcceptConnFromUpperNodeIpTableReuse(reuseport, localport, AgentStatus.Nodeid, AgentStatus.AESKey) } - go common.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) + go share.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) go HandleStartNodeConn(&ConnToAdmin, "", "", "", true, AgentStatus.Nodeid) if method == 1 { go node.StartNodeListenReuse(reusehost, reuseport, AgentStatus.Nodeid, AgentStatus.AESKey) @@ -204,7 +205,7 @@ func StartNodeReuseInit(reusehost, reuseport, localport string, method int) { } else { ProxyChan.ProxyChanToUpperNode <- NewNodeMessage if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -216,7 +217,7 @@ func StartNodeReuseInit(reusehost, reuseport, localport string, method int) { //reuseport下的普通节点 func SimpleNodeReuseInit(reusehost, reuseport, localport string, method int) { - AgentStatus.Nodeid = common.AdminId + AgentStatus.Nodeid = utils.AdminId if method == 1 { ConnToAdmin, AgentStatus.Nodeid = node.AcceptConnFromUpperNodeReuse(reusehost, reuseport, AgentStatus.Nodeid, AgentStatus.AESKey) } else { @@ -227,7 +228,7 @@ func SimpleNodeReuseInit(reusehost, reuseport, localport string, method int) { } ConnToAdmin, AgentStatus.Nodeid = node.AcceptConnFromUpperNodeIpTableReuse(reuseport, localport, AgentStatus.Nodeid, AgentStatus.AESKey) } - go common.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) + go share.SendHeartBeatControl(&ConnToAdmin, AgentStatus.Nodeid, AgentStatus.AESKey) go HandleSimpleNodeConn(&ConnToAdmin, AgentStatus.Nodeid) if method == 1 { go node.StartNodeListenReuse(reusehost, reuseport, AgentStatus.Nodeid, AgentStatus.AESKey) @@ -241,7 +242,7 @@ func SimpleNodeReuseInit(reusehost, reuseport, localport string, method int) { <-node.NodeStuff.IsAdmin ProxyChan.ProxyChanToUpperNode <- NewNodeMessage if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -280,7 +281,7 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri stdout io.Reader ) for { - AdminData, err := common.ExtractPayload(*connToAdmin, AgentStatus.AESKey, NODEID, false) + AdminData, err := utils.ExtractPayload(*connToAdmin, AgentStatus.AESKey, NODEID, false) if err != nil { AdminOffline(reConn, monitor, listenPort, passive) continue @@ -332,8 +333,8 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri ForwardConnMap.Unlock() PortFowardMap.Lock() if _, ok := PortFowardMap.Payload[AdminData.Clientid]; ok { - if !common.IsClosed(PortFowardMap.Payload[AdminData.Clientid]) { - if !common.IsClosed(PortFowardMap.Payload[AdminData.Clientid]) { + if !utils.IsClosed(PortFowardMap.Payload[AdminData.Clientid]) { + if !utils.IsClosed(PortFowardMap.Payload[AdminData.Clientid]) { close(PortFowardMap.Payload[AdminData.Clientid]) } delete(PortFowardMap.Payload, AdminData.Clientid) @@ -356,7 +357,7 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri case "FINOK": SocksDataChanMap.Lock() //性能损失? if _, ok := SocksDataChanMap.Payload[AdminData.Clientid]; ok { - if !common.IsClosed(SocksDataChanMap.Payload[AdminData.Clientid]) { + if !utils.IsClosed(SocksDataChanMap.Payload[AdminData.Clientid]) { close(SocksDataChanMap.Payload[AdminData.Clientid]) } delete(SocksDataChanMap.Payload, AdminData.Clientid) @@ -371,14 +372,14 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri CurrentConn.Unlock() SocksDataChanMap.Lock() if _, ok := SocksDataChanMap.Payload[AdminData.Clientid]; ok { - if !common.IsClosed(SocksDataChanMap.Payload[AdminData.Clientid]) { + if !utils.IsClosed(SocksDataChanMap.Payload[AdminData.Clientid]) { close(SocksDataChanMap.Payload[AdminData.Clientid]) } delete(SocksDataChanMap.Payload, AdminData.Clientid) } SocksDataChanMap.Unlock() case "HEARTBEAT": - hbdatapack, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "KEEPALIVE", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + hbdatapack, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "KEEPALIVE", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- hbdatapack default: continue @@ -431,44 +432,44 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri status = node.ConnectNextNode(addr, NODEID, AgentStatus.AESKey) } if !status { - message, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NODECONNECTFAIL", " ", "", 0, NODEID, AgentStatus.AESKey, false) + message, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NODECONNECTFAIL", " ", "", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- message } case "FILENAME": var err error UploadFile, err := os.Create(AdminData.Info) if err != nil { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "CREATEFAIL", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "CREATEFAIL", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm } else { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NAMECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NAMECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm - go common.ReceiveFile("", connToAdmin, FileDataMap, CannotRead, UploadFile, AgentStatus.AESKey, false, NODEID) + go share.ReceiveFile("", connToAdmin, FileDataMap, CannotRead, UploadFile, AgentStatus.AESKey, false, NODEID) } case "FILESIZE": filesize, _ := strconv.ParseInt(AdminData.Info, 10, 64) - common.File.FileSize = filesize - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "FILESIZECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + share.File.FileSize = filesize + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "FILESIZECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm - common.File.ReceiveFileSize <- true + share.File.ReceiveFileSize <- true case "FILESLICENUM": - common.File.TotalSilceNum, _ = strconv.Atoi(AdminData.Info) - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "FILESLICENUMCONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + share.File.TotalSilceNum, _ = strconv.Atoi(AdminData.Info) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "FILESLICENUMCONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm - common.File.ReceiveFileSliceNum <- true + share.File.ReceiveFileSliceNum <- true case "FILESLICENUMCONFIRM": - common.File.TotalConfirm <- true + share.File.TotalConfirm <- true case "FILESIZECONFIRM": - common.File.TotalConfirm <- true + share.File.TotalConfirm <- true case "DOWNLOADFILE": - go common.UploadFile("", AdminData.Info, connToAdmin, common.AdminId, GetName, AgentStatus.AESKey, NODEID, false) + go share.UploadFile("", AdminData.Info, connToAdmin, utils.AdminId, GetName, AgentStatus.AESKey, NODEID, false) case "NAMECONFIRM": GetName <- true case "CREATEFAIL": GetName <- false case "CANNOTREAD": CannotRead <- true - common.File.ReceiveFileSliceNum <- false + share.File.ReceiveFileSliceNum <- false os.Remove(AdminData.Info) //删除空文件 case "FORWARDTEST": go TestForward(AdminData.Info) @@ -490,10 +491,10 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri case "LISTEN": err := TestListen(AdminData.Info) if err != nil { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "LISTENRESP", " ", "FAILED", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "LISTENRESP", " ", "FAILED", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm } else { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "LISTENRESP", " ", "SUCCESS", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "LISTENRESP", " ", "SUCCESS", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm go node.StartNodeListen(AdminData.Info, NODEID, AgentStatus.AESKey) } @@ -507,12 +508,12 @@ func HandleConnFromAdmin(connToAdmin *net.Conn, monitor, listenPort, reConn stri if AdminData.Route == "" && AdminData.Command == "ID" { AgentStatus.WaitForIdAllocate <- AdminData.NodeId //将此节点序号递交,以便启动HandleConnFromLowerNode函数 node.NodeInfo.LowerNode.Lock() - node.NodeInfo.LowerNode.Payload[AdminData.NodeId] = node.NodeInfo.LowerNode.Payload[common.AdminId] + node.NodeInfo.LowerNode.Payload[AdminData.NodeId] = node.NodeInfo.LowerNode.Payload[utils.AdminId] node.NodeInfo.LowerNode.Unlock() } routeid := ChangeRoute(AdminData) //更改路由并返回下一个路由点 - proxyData, _ := common.ConstructPayload(AdminData.NodeId, AdminData.Route, AdminData.Type, AdminData.Command, AdminData.FileSliceNum, AdminData.Info, AdminData.Clientid, AdminData.CurrentId, AgentStatus.AESKey, true) - passToLowerData := common.NewPassToLowerNodeData() + proxyData, _ := utils.ConstructPayload(AdminData.NodeId, AdminData.Route, AdminData.Type, AdminData.Command, AdminData.FileSliceNum, AdminData.Info, AdminData.Clientid, AdminData.CurrentId, AgentStatus.AESKey, true) + passToLowerData := utils.NewPassToLowerNodeData() if routeid == "" { //当返回的路由点为0,说明就是自己的子节点 passToLowerData.Route = AdminData.NodeId } else { //不是0,说明不是自己的子节点,还需要一定轮数的递送 @@ -543,13 +544,13 @@ func HandleConnToLowerNode() { //看到那个from了么 func HandleConnFromLowerNode(connForLowerNode net.Conn, currentid, lowerid string) { for { - command, err := common.ExtractPayload(connForLowerNode, AgentStatus.AESKey, currentid, false) + command, err := utils.ExtractPayload(connForLowerNode, AgentStatus.AESKey, currentid, false) if err != nil { connForLowerNode.Close() node.NodeInfo.LowerNode.Lock() delete(node.NodeInfo.LowerNode.Payload, lowerid) //下级节点掉线,立即将此节点从自己的子节点列表删除 node.NodeInfo.LowerNode.Unlock() - offlineMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "AGENTOFFLINE", " ", lowerid, 0, currentid, AgentStatus.AESKey, false) //通知admin下级节点已经下线 + offlineMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "AGENTOFFLINE", " ", lowerid, 0, currentid, AgentStatus.AESKey, false) //通知admin下级节点已经下线 ProxyChan.ProxyChanToUpperNode <- offlineMess return } @@ -559,27 +560,27 @@ func HandleConnFromLowerNode(connForLowerNode net.Conn, currentid, lowerid strin case "RECONNID": if _, ok := node.NodeInfo.LowerNode.Payload[command.CurrentId]; ok { info := fmt.Sprintf("%s:::%s", currentid, connForLowerNode.RemoteAddr().String()) - proxyCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", command.Command, " ", info, 0, command.CurrentId, AgentStatus.AESKey, false) + proxyCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", command.Command, " ", info, 0, command.CurrentId, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- proxyCommand continue } else { - proxyCommand, _ := common.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) + proxyCommand, _ := utils.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) ProxyChan.ProxyChanToUpperNode <- proxyCommand continue } case "HEARTBEAT": - hbcommpack, _ := common.ConstructPayload(command.CurrentId, "", "COMMAND", "KEEPALIVE", " ", " ", 0, currentid, AgentStatus.AESKey, false) - passToLowerData := common.NewPassToLowerNodeData() + hbcommpack, _ := utils.ConstructPayload(command.CurrentId, "", "COMMAND", "KEEPALIVE", " ", " ", 0, currentid, AgentStatus.AESKey, false) + passToLowerData := utils.NewPassToLowerNodeData() passToLowerData.Data = hbcommpack passToLowerData.Route = command.CurrentId ProxyChan.ProxyChanToLowerNode <- passToLowerData continue default: - proxyData, _ := common.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) + proxyData, _ := utils.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) ProxyChan.ProxyChanToUpperNode <- proxyData } case "DATA": - proxyData, _ := common.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) + proxyData, _ := utils.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) ProxyChan.ProxyChanToUpperNode <- proxyData } } @@ -615,7 +616,7 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { stdout io.Reader ) for { - command, err := common.ExtractPayload(*connToUpperNode, AgentStatus.AESKey, NODEID, false) + command, err := utils.ExtractPayload(*connToUpperNode, AgentStatus.AESKey, NODEID, false) if err != nil { node.NodeStuff.Offline = true WaitingAdmin(NODEID) //上一级节点间网络连接断开后不掉线,等待上级节点重连回来 @@ -671,43 +672,43 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { status = node.ConnectNextNode(addr, NODEID, AgentStatus.AESKey) } if !status { - message, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NODECONNECTFAIL", " ", "", 0, NODEID, AgentStatus.AESKey, false) + message, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NODECONNECTFAIL", " ", "", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- message } case "FILENAME": var err error UploadFile, err := os.Create(command.Info) if err != nil { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "CREATEFAIL", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "CREATEFAIL", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm } else { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NAMECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NAMECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm - go common.ReceiveFile("", connToUpperNode, FileDataMap, CannotRead, UploadFile, AgentStatus.AESKey, false, NODEID) + go share.ReceiveFile("", connToUpperNode, FileDataMap, CannotRead, UploadFile, AgentStatus.AESKey, false, NODEID) } case "FILESIZE": - common.File.FileSize, _ = strconv.ParseInt(command.Info, 10, 64) - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "FILESIZECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + share.File.FileSize, _ = strconv.ParseInt(command.Info, 10, 64) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "FILESIZECONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm - common.File.ReceiveFileSize <- true + share.File.ReceiveFileSize <- true case "FILESLICENUM": - common.File.TotalSilceNum, _ = strconv.Atoi(command.Info) - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "FILESLICENUMCONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) + share.File.TotalSilceNum, _ = strconv.Atoi(command.Info) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "FILESLICENUMCONFIRM", " ", " ", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm - common.File.ReceiveFileSliceNum <- true + share.File.ReceiveFileSliceNum <- true case "FILESLICENUMCONFIRM": - common.File.TotalConfirm <- true + share.File.TotalConfirm <- true case "FILESIZECONFIRM": - common.File.TotalConfirm <- true + share.File.TotalConfirm <- true case "DOWNLOADFILE": - go common.UploadFile("", command.Info, connToUpperNode, common.AdminId, GetName, AgentStatus.AESKey, NODEID, false) + go share.UploadFile("", command.Info, connToUpperNode, utils.AdminId, GetName, AgentStatus.AESKey, NODEID, false) case "NAMECONFIRM": GetName <- true case "CREATEFAIL": GetName <- false case "CANNOTREAD": CannotRead <- true - common.File.ReceiveFileSliceNum <- false + share.File.ReceiveFileSliceNum <- false os.Remove(command.Info) case "FORWARDTEST": go TestForward(command.Info) @@ -727,24 +728,24 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { listener.Close() } case "RECONN": - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "RECONNID", " ", "", 0, NODEID, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "RECONNID", " ", "", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand if AgentStatus.NotLastOne { BroadCast("RECONN") } case "CLEAR": ClearAllConn() - SocksDataChanMap = common.NewUint32ChanStrMap() + SocksDataChanMap = utils.NewUint32ChanStrMap() if AgentStatus.NotLastOne { BroadCast("CLEAR") } case "LISTEN": err := TestListen(command.Info) if err != nil { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "LISTENRESP", " ", "FAILED", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "LISTENRESP", " ", "FAILED", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm } else { - respComm, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "LISTENRESP", " ", "SUCCESS", 0, NODEID, AgentStatus.AESKey, false) + respComm, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "LISTENRESP", " ", "SUCCESS", 0, NODEID, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respComm go node.StartNodeListen(command.Info, NODEID, AgentStatus.AESKey) } @@ -770,7 +771,7 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { case "FINOK": SocksDataChanMap.Lock() if _, ok := SocksDataChanMap.Payload[command.Clientid]; ok { - if !common.IsClosed(SocksDataChanMap.Payload[command.Clientid]) { + if !utils.IsClosed(SocksDataChanMap.Payload[command.Clientid]) { close(SocksDataChanMap.Payload[command.Clientid]) } delete(SocksDataChanMap.Payload, command.Clientid) @@ -790,7 +791,7 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { CurrentConn.Unlock() SocksDataChanMap.Lock() if _, ok := SocksDataChanMap.Payload[command.Clientid]; ok { - if !common.IsClosed(SocksDataChanMap.Payload[command.Clientid]) { + if !utils.IsClosed(SocksDataChanMap.Payload[command.Clientid]) { close(SocksDataChanMap.Payload[command.Clientid]) } delete(SocksDataChanMap.Payload, command.Clientid) @@ -821,7 +822,7 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { ForwardConnMap.Unlock() PortFowardMap.Lock() if _, ok := PortFowardMap.Payload[command.Clientid]; ok { - if !common.IsClosed(PortFowardMap.Payload[command.Clientid]) { + if !utils.IsClosed(PortFowardMap.Payload[command.Clientid]) { close(PortFowardMap.Payload[command.Clientid]) } } @@ -847,12 +848,12 @@ func HandleConnFromUpperNode(connToUpperNode *net.Conn, NODEID string) { if command.Route == "" && command.Command == "ID" { AgentStatus.WaitForIdAllocate <- command.NodeId node.NodeInfo.LowerNode.Lock() - node.NodeInfo.LowerNode.Payload[command.NodeId] = node.NodeInfo.LowerNode.Payload[common.AdminId] + node.NodeInfo.LowerNode.Payload[command.NodeId] = node.NodeInfo.LowerNode.Payload[utils.AdminId] node.NodeInfo.LowerNode.Unlock() } routeid := ChangeRoute(command) - proxyData, _ := common.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) - passToLowerData := common.NewPassToLowerNodeData() + proxyData, _ := utils.ConstructPayload(command.NodeId, command.Route, command.Type, command.Command, command.FileSliceNum, command.Info, command.Clientid, command.CurrentId, AgentStatus.AESKey, true) + passToLowerData := utils.NewPassToLowerNodeData() if routeid == "" { passToLowerData.Route = command.NodeId } else { diff --git a/agent/command.go b/agent/command.go index f74e567..2f61b7f 100644 --- a/agent/command.go +++ b/agent/command.go @@ -1,16 +1,16 @@ package agent import ( - "Stowaway/common" + "Stowaway/utils" "flag" "log" "os" ) -var Args *common.AgentOptions +var Args *utils.AgentOptions func init() { - Args = new(common.AgentOptions) + Args = new(utils.AgentOptions) flag.StringVar(&Args.Secret, "s", "", "") flag.StringVar(&Args.Listen, "l", "", "") flag.StringVar(&Args.Reconnect, "reconnect", "", "") diff --git a/agent/forward.go b/agent/forward.go index 10af224..f02a32d 100644 --- a/agent/forward.go +++ b/agent/forward.go @@ -1,18 +1,18 @@ package agent import ( - "Stowaway/common" + "Stowaway/utils" "net" ) var ( - PortFowardMap *common.Uint32ChanStrMap - ForwardConnMap *common.Uint32ConnMap + PortFowardMap *utils.Uint32ChanStrMap + ForwardConnMap *utils.Uint32ConnMap ) func init() { - PortFowardMap = common.NewUint32ChanStrMap() - ForwardConnMap = common.NewUint32ConnMap() + PortFowardMap = utils.NewUint32ChanStrMap() + ForwardConnMap = utils.NewUint32ConnMap() } /*-------------------------Port-forward启动相关代码--------------------------*/ @@ -20,11 +20,11 @@ func init() { func TestForward(target string) { forwardConn, err := net.Dial("tcp", target) if err != nil { - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "FORWARDFAIL", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "FORWARDFAIL", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand } else { defer forwardConn.Close() - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "FORWARDOK", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "FORWARDOK", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand } @@ -38,7 +38,7 @@ func TryForward(target string, num uint32) { ForwardConnMap.Payload[num] = forwardConn ForwardConnMap.Unlock() } else { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "FORWARDTIMEOUT", " ", " ", num, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "FORWARDTIMEOUT", " ", " ", num, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respdata return } @@ -66,11 +66,11 @@ func HandleForward(forwardDataChan chan string, forwardNum uint32) { for { len, err := forwardConn.Read(serverbuffer) if err != nil { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "FORWARDOFFLINE", " ", " ", forwardNum, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "FORWARDOFFLINE", " ", " ", forwardNum, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respdata return } - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "FORWARDDATARESP", " ", string(serverbuffer[:len]), forwardNum, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "FORWARDDATARESP", " ", string(serverbuffer[:len]), forwardNum, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respdata } }() diff --git a/agent/function.go b/agent/function.go index f02606c..73de34a 100644 --- a/agent/function.go +++ b/agent/function.go @@ -1,8 +1,8 @@ package agent import ( - "Stowaway/common" "Stowaway/node" + "Stowaway/utils" "errors" "fmt" "log" @@ -39,7 +39,7 @@ func AdminOffline(reConn, monitor, listenPort string, passive bool) { if reConn != "0" && reConn != "" && !passive { ClearAllConn() time.Sleep(1 * time.Second) - SocksDataChanMap = common.NewUint32ChanStrMap() + SocksDataChanMap = utils.NewUint32ChanStrMap() if AgentStatus.NotLastOne { BroadCast("CLEAR") } @@ -50,7 +50,7 @@ func AdminOffline(reConn, monitor, listenPort string, passive bool) { } else if passive { ClearAllConn() time.Sleep(1 * time.Second) - SocksDataChanMap = common.NewUint32ChanStrMap() + SocksDataChanMap = utils.NewUint32ChanStrMap() if AgentStatus.NotLastOne { BroadCast("CLEAR") } @@ -68,13 +68,13 @@ func AdminOffline(reConn, monitor, listenPort string, passive bool) { func WaitingAdmin(nodeid string) { //清理工作 ClearAllConn() - SocksDataChanMap = common.NewUint32ChanStrMap() + SocksDataChanMap = utils.NewUint32ChanStrMap() if AgentStatus.NotLastOne { BroadCast("CLEAR") } //等待重连 ConnToAdmin = <-node.NodeStuff.Adminconn - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "RECONNID", " ", "", 0, nodeid, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "RECONNID", " ", "", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand if AgentStatus.NotLastOne { BroadCast("RECONN") @@ -89,7 +89,7 @@ func PrepareForReOnlineNode() { conn := <-node.NodeStuff.ReOnlineConn //如果此节点没有启动过HandleConnToLowerNode函数,启动之 if AgentStatus.NotLastOne == false { - ProxyChan.ProxyChanToLowerNode = make(chan *common.PassToLowerNodeData) + ProxyChan.ProxyChanToLowerNode = make(chan *utils.PassToLowerNodeData) go HandleConnToLowerNode() } AgentStatus.NotLastOne = true @@ -125,7 +125,7 @@ func ClearAllConn() { SocksDataChanMap.Lock() for key, _ := range SocksDataChanMap.Payload { - if !common.IsClosed(SocksDataChanMap.Payload[key]) { + if !utils.IsClosed(SocksDataChanMap.Payload[key]) { close(SocksDataChanMap.Payload[key]) } delete(SocksDataChanMap.Payload, key) @@ -134,7 +134,7 @@ func ClearAllConn() { PortFowardMap.Lock() for key, _ := range PortFowardMap.Payload { - if !common.IsClosed(PortFowardMap.Payload[key]) { + if !utils.IsClosed(PortFowardMap.Payload[key]) { close(PortFowardMap.Payload[key]) } delete(PortFowardMap.Payload, key) @@ -166,7 +166,7 @@ func ClearAllConn() { } //查找需要递交的路由 -func ChangeRoute(AdminData *common.Payload) string { +func ChangeRoute(AdminData *utils.Payload) string { route := AdminData.Route routes := strings.Split(route, ":") selected := routes[0] @@ -179,7 +179,7 @@ func BroadCast(command string) { var readyToBroadCast []string node.NodeInfo.LowerNode.Lock() for nodeid, _ := range node.NodeInfo.LowerNode.Payload { - if nodeid == common.AdminId { + if nodeid == utils.AdminId { continue } readyToBroadCast = append(readyToBroadCast, nodeid) @@ -187,8 +187,8 @@ func BroadCast(command string) { node.NodeInfo.LowerNode.Unlock() for _, nodeid := range readyToBroadCast { - mess, _ := common.ConstructPayload(nodeid, "", "COMMAND", command, " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) - passToLowerData := common.NewPassToLowerNodeData() + mess, _ := utils.ConstructPayload(nodeid, "", "COMMAND", command, " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + passToLowerData := utils.NewPassToLowerNodeData() passToLowerData.Data = mess passToLowerData.Route = nodeid ProxyChan.ProxyChanToLowerNode <- passToLowerData diff --git a/agent/reflect.go b/agent/reflect.go index de00032..2ddec8c 100644 --- a/agent/reflect.go +++ b/agent/reflect.go @@ -1,21 +1,21 @@ package agent import ( - "Stowaway/common" + "Stowaway/utils" "fmt" "net" "strings" ) var ( - ReflectConnMap *common.Uint32ConnMap + ReflectConnMap *utils.Uint32ConnMap CurrentPortReflectListener []net.Listener - ReflectStatus *common.ReflectStatus + ReflectStatus *utils.ReflectStatus ) func init() { - ReflectStatus = common.NewReflectStatus() - ReflectConnMap = common.NewUint32ConnMap() + ReflectStatus = utils.NewReflectStatus() + ReflectConnMap = utils.NewUint32ConnMap() } /*-------------------------Port-reflect启动相关代码--------------------------*/ @@ -26,12 +26,12 @@ func TestReflect(portCombine string) { reflectAddr := fmt.Sprintf("0.0.0.0:%s", ports[1]) reflectListenerForClient, err := net.Listen("tcp", reflectAddr) if err != nil { - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "REFLECTFAIL", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "REFLECTFAIL", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand return } else { defer reflectListenerForClient.Close() - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "REFLECTOK", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "REFLECTOK", " ", " ", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand } @@ -42,10 +42,10 @@ func TestReflect(portCombine string) { if err != nil { return } else { - respCommand, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "GETREFLECTNUM", " ", ports[0], 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respCommand, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "GETREFLECTNUM", " ", ports[0], 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand num = <-ReflectStatus.ReflectNum - respCommand, _ = common.ConstructPayload(common.AdminId, "", "DATA", "REFLECT", " ", ports[0], num, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respCommand, _ = utils.ConstructPayload(utils.AdminId, "", "DATA", "REFLECT", " ", ports[0], num, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respCommand } ReflectConnMap.Lock() @@ -61,11 +61,11 @@ func HandleReflectPort(reflectconn net.Conn, num uint32, nodeid string) { for { len, err := reflectconn.Read(buffer) if err != nil { - finMessage, _ := common.ConstructPayload(common.AdminId, "", "DATA", "REFLECTFIN", " ", " ", num, AgentStatus.Nodeid, AgentStatus.AESKey, false) + finMessage, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "REFLECTFIN", " ", " ", num, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- finMessage return } else { - respData, _ := common.ConstructPayload(common.AdminId, "", "DATA", "REFLECTDATA", " ", string(buffer[:len]), num, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respData, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "REFLECTDATA", " ", string(buffer[:len]), num, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respData } } diff --git a/agent/shell.go b/agent/shell.go index 7679487..0660517 100644 --- a/agent/shell.go +++ b/agent/shell.go @@ -1,7 +1,7 @@ package agent import ( - "Stowaway/common" + "Stowaway/utils" "fmt" "io" "os/exec" @@ -11,7 +11,7 @@ import ( //创建交互式shell func CreatInteractiveShell() (io.Reader, io.Writer) { var cmd *exec.Cmd - sys := common.CheckSystem() + sys := utils.CheckSystem() switch sys { case 0x01: cmd = exec.Command("c:\\windows\\system32\\cmd.exe") @@ -43,7 +43,7 @@ func StartShell(command string, stdin io.Writer, stdout io.Reader, currentid str if err != nil { return } - respShell, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SHELLRESP", " ", string(buf[:count]), 0, currentid, AgentStatus.AESKey, false) + respShell, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SHELLRESP", " ", string(buf[:count]), 0, currentid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respShell } } diff --git a/agent/socks.go b/agent/socks.go index 6dc81f1..d93216e 100644 --- a/agent/socks.go +++ b/agent/socks.go @@ -1,21 +1,20 @@ package agent import ( - "Stowaway/common" - "Stowaway/socks" + "Stowaway/utils" "net" ) -var CurrentConn *common.Uint32ConnMap +var CurrentConn *utils.Uint32ConnMap func init() { - CurrentConn = common.NewUint32ConnMap() + CurrentConn = utils.NewUint32ConnMap() } /*-------------------------Socks启动相关代码--------------------------*/ //暂时没啥用,仅做回复socks开启命令之用 func StartSocks() { - socksstartmess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SOCKSRESP", " ", "SUCCESS", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + socksstartmess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SOCKSRESP", " ", "SUCCESS", 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- socksstartmess } @@ -34,7 +33,7 @@ func HanleClientSocksConn(info chan string, socksUsername, socksPass string, che if !ok { //重连后原先引用失效,当chan释放后,若不捕捉,会无限循环 return } - method = socks.CheckMethod(ConnToAdmin, []byte(data), socksUsername, socksPass, checknum, AgentStatus.AESKey, AgentStatus.Nodeid) + method = CheckMethod(ConnToAdmin, []byte(data), socksUsername, socksPass, checknum, AgentStatus.AESKey, AgentStatus.Nodeid) if method == "NONE" { isAuthed = true } @@ -43,13 +42,13 @@ func HanleClientSocksConn(info chan string, socksUsername, socksPass string, che if !ok { return } - isAuthed = socks.AuthClient(ConnToAdmin, []byte(data), socksUsername, socksPass, checknum, AgentStatus.AESKey, AgentStatus.Nodeid) + isAuthed = AuthClient(ConnToAdmin, []byte(data), socksUsername, socksPass, checknum, AgentStatus.AESKey, AgentStatus.Nodeid) } else if isAuthed == true && tcpconnected == false { data, ok := <-info if !ok { return } - server, tcpconnected, serverflag = socks.ConfirmTarget(ConnToAdmin, []byte(data), checknum, AgentStatus.AESKey, AgentStatus.Nodeid) + server, tcpconnected, serverflag = ConfirmTarget(ConnToAdmin, []byte(data), checknum, AgentStatus.AESKey, AgentStatus.Nodeid) if serverflag == false { return } @@ -76,7 +75,7 @@ func HanleClientSocksConn(info chan string, socksUsername, socksPass string, che } } }() - err := socks.Proxyhttp(ConnToAdmin, server, checknum, AgentStatus.AESKey, currentid) + err := Proxyhttp(ConnToAdmin, server, checknum, AgentStatus.AESKey, currentid) if err != nil { go SendFin(checknum) return @@ -92,7 +91,7 @@ func SendFin(num uint32) { SocksDataChanMap.RLock() if _, ok := SocksDataChanMap.Payload[num]; ok { SocksDataChanMap.RUnlock() - respData, _ := common.ConstructPayload(common.AdminId, "", "DATA", "FIN", " ", " ", num, AgentStatus.Nodeid, AgentStatus.AESKey, false) + respData, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "FIN", " ", " ", num, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- respData return } else { diff --git a/socks/socks.go b/agent/socksfunc.go similarity index 69% rename from socks/socks.go rename to agent/socksfunc.go index d86264c..7526dd5 100644 --- a/socks/socks.go +++ b/agent/socksfunc.go @@ -1,7 +1,7 @@ -package socks +package agent import ( - "Stowaway/common" + "Stowaway/utils" "fmt" "log" "net" @@ -13,19 +13,19 @@ import ( func CheckMethod(conntoupper net.Conn, buffer []byte, username string, secret string, clientid uint32, key []byte, currentid string) string { if buffer[0] == 0x05 { if buffer[2] == 0x02 && (username != "") { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x02}), clientid, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x02}), clientid, currentid, key, false) conntoupper.Write(respdata) return "PASSWORD" } else if buffer[2] == 0x00 && (username == "" && secret == "") { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x00}), clientid, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x00}), clientid, currentid, key, false) conntoupper.Write(respdata) return "NONE" } else if buffer[2] == 0x00 && (username != "") { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x02}), clientid, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x02}), clientid, currentid, key, false) conntoupper.Write(respdata) return "ILLEGAL" } else if buffer[2] == 0x02 && (username == "") { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x00}), clientid, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x00}), clientid, currentid, key, false) conntoupper.Write(respdata) return "ILLEGAL" } @@ -41,11 +41,11 @@ func AuthClient(conntoupper net.Conn, buffer []byte, username string, secret str clientpass := string(buffer[3+ulen : 3+ulen+slen]) if clientname != username || clientpass != secret { log.Println("Illegal client!") - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x01, 0x01}), clientid, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x01, 0x01}), clientid, currentid, key, false) conntoupper.Write(respdata) return false } else { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x01, 0x00}), clientid, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x01, 0x00}), clientid, currentid, key, false) conntoupper.Write(respdata) return true } @@ -89,11 +89,11 @@ func TcpConnect(conntoupper net.Conn, buffer []byte, len int, checknum uint32, k port := strconv.Itoa(int(buffer[len-2])<<8 | int(buffer[len-1])) server, err := net.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), checknum, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), checknum, currentid, key, false) conntoupper.Write(respdata) return server, false, false } - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), checknum, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), checknum, currentid, key, false) conntoupper.Write(respdata) return server, true, true } @@ -107,7 +107,7 @@ func Proxyhttp(conntoupper net.Conn, server net.Conn, checknum uint32, key []byt server.Close() return err } - respdata, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SOCKSDATARESP", " ", string(serverbuffer[:len]), checknum, currentid, key, false) + respdata, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SOCKSDATARESP", " ", string(serverbuffer[:len]), checknum, currentid, key, false) conntoupper.Write(respdata) } return nil diff --git a/agent/ssh.go b/agent/ssh.go index 081c4e3..d5679e6 100644 --- a/agent/ssh.go +++ b/agent/ssh.go @@ -1,7 +1,7 @@ package agent import ( - "Stowaway/common" + "Stowaway/utils" "fmt" "io" "strings" @@ -29,7 +29,7 @@ func StartSSH(info string, nodeid string) error { } else if method == "2" { key, err := ssh.ParsePrivateKey([]byte(authway)) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHCERTERROR", " ", " ", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHCERTERROR", " ", " ", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return err } @@ -42,14 +42,14 @@ func StartSSH(info string, nodeid string) error { HostKeyCallback: ssh.InsecureIgnoreHostKey(), }) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return err } Sshhost, err = sshdial.NewSession() if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return err } @@ -65,7 +65,7 @@ func StartSSH(info string, nodeid string) error { } Sshhost.Stderr = Sshhost.Stdout Sshhost.Shell() - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHRESP", " ", "SUCCESS", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHRESP", " ", "SUCCESS", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return nil } @@ -81,7 +81,7 @@ func ReadCommand() { if err != nil { break } - sshRespMess, _ := common.ConstructPayload(common.AdminId, "", "DATA", "SSHMESS", " ", string(buffer[:len]), 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) + sshRespMess, _ := utils.ConstructPayload(utils.AdminId, "", "DATA", "SSHMESS", " ", string(buffer[:len]), 0, AgentStatus.Nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshRespMess } } diff --git a/agent/sshtunnel.go b/agent/sshtunnel.go index e75fbc7..f8d882d 100644 --- a/agent/sshtunnel.go +++ b/agent/sshtunnel.go @@ -1,8 +1,8 @@ package agent import ( - "Stowaway/common" "Stowaway/node" + "Stowaway/utils" "fmt" "strings" @@ -24,7 +24,7 @@ func SshTunnelNextNode(info string, nodeid string) error { } else if method == "2" { key, err := ssh.ParsePrivateKey([]byte(authway)) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHCERTERROR", " ", " ", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHCERTERROR", " ", " ", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return err } @@ -37,7 +37,7 @@ func SshTunnelNextNode(info string, nodeid string) error { HostKeyCallback: ssh.InsecureIgnoreHostKey(), }) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return err } @@ -45,7 +45,7 @@ func SshTunnelNextNode(info string, nodeid string) error { nodeConn, err := SshClient.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", lport)) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess nodeConn.Close() return err @@ -53,30 +53,30 @@ func SshTunnelNextNode(info string, nodeid string) error { err = node.SendSecret(nodeConn, AgentStatus.AESKey) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess nodeConn.Close() return err } - helloMess, _ := common.ConstructPayload(nodeid, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, common.AdminId, AgentStatus.AESKey, false) + helloMess, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, utils.AdminId, AgentStatus.AESKey, false) nodeConn.Write(helloMess) for { - command, err := common.ExtractPayload(nodeConn, AgentStatus.AESKey, common.AdminId, true) + command, err := utils.ExtractPayload(nodeConn, AgentStatus.AESKey, utils.AdminId, true) if err != nil { - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "FAILED", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess nodeConn.Close() return err } switch command.Command { case "INIT": - NewNodeMessage, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NEW", " ", nodeConn.RemoteAddr().String(), 0, nodeid, AgentStatus.AESKey, false) - node.NodeInfo.LowerNode.Payload[common.AdminId] = nodeConn + NewNodeMessage, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NEW", " ", nodeConn.RemoteAddr().String(), 0, nodeid, AgentStatus.AESKey, false) + node.NodeInfo.LowerNode.Payload[utils.AdminId] = nodeConn node.NodeStuff.ControlConnForLowerNodeChan <- nodeConn node.NodeStuff.NewNodeMessageChan <- NewNodeMessage node.NodeStuff.IsAdmin <- false - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "SUCCESS", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "SUCCESS", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return nil case "REONLINE": @@ -84,9 +84,9 @@ func SshTunnelNextNode(info string, nodeid string) error { node.NodeStuff.ReOnlineId <- command.CurrentId node.NodeStuff.ReOnlineConn <- nodeConn <-node.NodeStuff.PrepareForReOnlineNodeReady - NewNodeMessage, _ := common.ConstructPayload(nodeid, "", "COMMAND", "REONLINESUC", " ", " ", 0, nodeid, AgentStatus.AESKey, false) + NewNodeMessage, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "REONLINESUC", " ", " ", 0, nodeid, AgentStatus.AESKey, false) nodeConn.Write(NewNodeMessage) - sshMess, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "SUCCESS", 0, nodeid, AgentStatus.AESKey, false) + sshMess, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "SSHTUNNELRESP", " ", "SUCCESS", 0, nodeid, AgentStatus.AESKey, false) ProxyChan.ProxyChanToUpperNode <- sshMess return nil } diff --git a/node/init.go b/node/init.go index 8629b0e..9d93c00 100644 --- a/node/init.go +++ b/node/init.go @@ -1,7 +1,7 @@ package node import ( - "Stowaway/common" + "Stowaway/utils" "errors" "fmt" "io" @@ -12,13 +12,13 @@ import ( ) var ( - NodeInfo *common.NodeInfo - NodeStuff *common.NodeStuff + NodeInfo *utils.NodeInfo + NodeStuff *utils.NodeStuff ) func init() { - NodeStuff = common.NewNodeStuff() - NodeInfo = common.NewNodeInfo() + NodeStuff = utils.NewNodeStuff() + NodeInfo = utils.NewNodeInfo() } /*-------------------------一般模式下初始化节点代码--------------------------*/ @@ -36,12 +36,12 @@ func StartNodeConn(monitor string, listenPort string, nodeID string, key []byte) return controlConnToUpperNode, "", err } - helloMess, _ := common.ConstructPayload(nodeID, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, common.AdminId, key, false) + helloMess, _ := utils.ConstructPayload(nodeID, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, utils.AdminId, key, false) controlConnToUpperNode.Write(helloMess) - common.ExtractPayload(controlConnToUpperNode, key, common.AdminId, true) + utils.ExtractPayload(controlConnToUpperNode, key, utils.AdminId, true) - respcommand, _ := common.ConstructPayload(nodeID, "", "COMMAND", "INIT", " ", listenPort, 0, common.AdminId, key, false) //主动向上级节点发送初始信息 + respcommand, _ := utils.ConstructPayload(nodeID, "", "COMMAND", "INIT", " ", listenPort, 0, utils.AdminId, key, false) //主动向上级节点发送初始信息 _, err = controlConnToUpperNode.Write(respcommand) if err != nil { log.Printf("[*]Error occured: %s", err) @@ -49,7 +49,7 @@ func StartNodeConn(monitor string, listenPort string, nodeID string, key []byte) } //等待admin为其分配一个id号 for { - command, _ := common.ExtractPayload(controlConnToUpperNode, key, common.AdminId, true) + command, _ := utils.ExtractPayload(controlConnToUpperNode, key, utils.AdminId, true) switch command.Command { case "ID": nodeID = command.NodeId @@ -87,10 +87,10 @@ func StartNodeListen(listenPort string, NodeId string, key []byte) { } for i := 0; i < 2; i++ { - command, _ := common.ExtractPayload(ConnToLowerNode, key, common.AdminId, true) + command, _ := utils.ExtractPayload(ConnToLowerNode, key, utils.AdminId, true) switch command.Command { case "STOWAWAYADMIN": - respcommand, _ := common.ConstructPayload(NodeId, "", "COMMAND", "INIT", " ", listenPort, 0, common.AdminId, key, false) + respcommand, _ := utils.ConstructPayload(NodeId, "", "COMMAND", "INIT", " ", listenPort, 0, utils.AdminId, key, false) ConnToLowerNode.Write(respcommand) case "ID": NodeStuff.ControlConnForLowerNodeChan <- ConnToLowerNode @@ -100,16 +100,16 @@ func StartNodeListen(listenPort string, NodeId string, key []byte) { NodeStuff.Adminconn <- ConnToLowerNode case "STOWAWAYAGENT": if !NodeStuff.Offline { - NewNodeMessage, _ = common.ConstructPayload(NodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, NodeId, key, false) + NewNodeMessage, _ = utils.ConstructPayload(NodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, NodeId, key, false) ConnToLowerNode.Write(NewNodeMessage) } else { - respcommand, _ := common.ConstructPayload(NodeId, "", "COMMAND", "REONLINE", " ", listenPort, 0, NodeId, key, false) + respcommand, _ := utils.ConstructPayload(NodeId, "", "COMMAND", "REONLINE", " ", listenPort, 0, NodeId, key, false) ConnToLowerNode.Write(respcommand) } case "INIT": //告知admin新节点消息 - NewNodeMessage, _ = common.ConstructPayload(common.AdminId, "", "COMMAND", "NEW", " ", ConnToLowerNode.RemoteAddr().String(), 0, NodeId, key, false) - NodeInfo.LowerNode.Payload[common.AdminId] = ConnToLowerNode //将这个socket用0号位暂存,等待admin分配完id后再将其放入对应的位置 + NewNodeMessage, _ = utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NEW", " ", ConnToLowerNode.RemoteAddr().String(), 0, NodeId, key, false) + NodeInfo.LowerNode.Payload[utils.AdminId] = ConnToLowerNode //将这个socket用0号位暂存,等待admin分配完id后再将其放入对应的位置 NodeStuff.ControlConnForLowerNodeChan <- ConnToLowerNode NodeStuff.NewNodeMessageChan <- NewNodeMessage //被连接后不终止监听,继续等待可能的后续节点连接,以此组成树状结构 NodeStuff.IsAdmin <- false @@ -133,11 +133,11 @@ func ConnectNextNode(target string, nodeid string, key []byte) bool { return false } - helloMess, _ := common.ConstructPayload(nodeid, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, common.AdminId, key, false) + helloMess, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, utils.AdminId, key, false) controlConnToNextNode.Write(helloMess) for { - command, err := common.ExtractPayload(controlConnToNextNode, key, common.AdminId, true) + command, err := utils.ExtractPayload(controlConnToNextNode, key, utils.AdminId, true) if err != nil { log.Println("[*]", err) return false @@ -146,8 +146,8 @@ func ConnectNextNode(target string, nodeid string, key []byte) bool { switch command.Command { case "INIT": //类似与上面 - NewNodeMessage, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NEW", " ", controlConnToNextNode.RemoteAddr().String(), 0, nodeid, key, false) - NodeInfo.LowerNode.Payload[common.AdminId] = controlConnToNextNode + NewNodeMessage, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NEW", " ", controlConnToNextNode.RemoteAddr().String(), 0, nodeid, key, false) + NodeInfo.LowerNode.Payload[utils.AdminId] = controlConnToNextNode NodeStuff.ControlConnForLowerNodeChan <- controlConnToNextNode NodeStuff.NewNodeMessageChan <- NewNodeMessage NodeStuff.IsAdmin <- false @@ -157,7 +157,7 @@ func ConnectNextNode(target string, nodeid string, key []byte) bool { NodeStuff.ReOnlineId <- command.CurrentId NodeStuff.ReOnlineConn <- controlConnToNextNode <-NodeStuff.PrepareForReOnlineNodeReady - NewNodeMessage, _ := common.ConstructPayload(nodeid, "", "COMMAND", "REONLINESUC", " ", " ", 0, nodeid, key, false) + NewNodeMessage, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "REONLINESUC", " ", " ", 0, nodeid, key, false) controlConnToNextNode.Write(NewNodeMessage) return true } @@ -186,12 +186,12 @@ func AcceptConnFromUpperNode(listenPort string, nodeid string, key []byte) (net. continue } - common.ExtractPayload(Comingconn, key, common.AdminId, true) + utils.ExtractPayload(Comingconn, key, utils.AdminId, true) - respcommand, _ := common.ConstructPayload(nodeid, "", "COMMAND", "INIT", " ", listenPort, 0, common.AdminId, key, false) + respcommand, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "INIT", " ", listenPort, 0, utils.AdminId, key, false) Comingconn.Write(respcommand) - command, _ := common.ExtractPayload(Comingconn, key, common.AdminId, true) //等待分配id + command, _ := utils.ExtractPayload(Comingconn, key, utils.AdminId, true) //等待分配id if command.Command == "ID" { nodeid = command.NodeId WaitingForConn.Close() @@ -208,7 +208,7 @@ func SendSecret(conn net.Conn, key []byte) error { var NOT_VALID = errors.New("not valid") defer conn.SetReadDeadline(time.Time{}) conn.SetReadDeadline(time.Now().Add(10 * time.Second)) - secret := common.GetStringMd5(string(key)) + secret := utils.GetStringMd5(string(key)) conn.Write([]byte(secret[:16])) buffer := make([]byte, 16) @@ -236,7 +236,7 @@ func CheckSecret(conn net.Conn, key []byte) error { var NOT_VALID = errors.New("not valid") defer conn.SetReadDeadline(time.Time{}) conn.SetReadDeadline(time.Now().Add(10 * time.Second)) - secret := common.GetStringMd5(string(key)) + secret := utils.GetStringMd5(string(key)) buffer := make([]byte, 16) count, err := io.ReadFull(conn, buffer) diff --git a/node/iptables_reuse.go b/node/iptables_reuse.go index 3bcfb30..7c4bda0 100644 --- a/node/iptables_reuse.go +++ b/node/iptables_reuse.go @@ -1,7 +1,7 @@ package node import ( - "Stowaway/common" + "Stowaway/utils" "fmt" "log" "net" @@ -38,12 +38,12 @@ func AcceptConnFromUpperNodeIpTableReuse(report, localport string, nodeid string continue } - common.ExtractPayload(Comingconn, key, common.AdminId, true) + utils.ExtractPayload(Comingconn, key, utils.AdminId, true) - respcommand, _ := common.ConstructPayload(nodeid, "", "COMMAND", "INIT", " ", report, 0, common.AdminId, key, false) + respcommand, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "INIT", " ", report, 0, utils.AdminId, key, false) Comingconn.Write(respcommand) - command, _ := common.ExtractPayload(Comingconn, key, common.AdminId, true) //等待分配id + command, _ := utils.ExtractPayload(Comingconn, key, utils.AdminId, true) //等待分配id if command.Command == "ID" { nodeid = command.NodeId WaitingForConn.Close() @@ -82,10 +82,10 @@ func StartNodeListenIpTableReuse(report, localport string, NodeId string, key [] } for i := 0; i < 2; i++ { - command, _ := common.ExtractPayload(ConnToLowerNode, key, common.AdminId, true) + command, _ := utils.ExtractPayload(ConnToLowerNode, key, utils.AdminId, true) switch command.Command { case "STOWAWAYADMIN": - respcommand, _ := common.ConstructPayload(NodeId, "", "COMMAND", "INIT", " ", report, 0, common.AdminId, key, false) + respcommand, _ := utils.ConstructPayload(NodeId, "", "COMMAND", "INIT", " ", report, 0, utils.AdminId, key, false) ConnToLowerNode.Write(respcommand) case "ID": NodeStuff.ControlConnForLowerNodeChan <- ConnToLowerNode @@ -95,15 +95,15 @@ func StartNodeListenIpTableReuse(report, localport string, NodeId string, key [] NodeStuff.Adminconn <- ConnToLowerNode case "STOWAWAYAGENT": if !NodeStuff.Offline { - NewNodeMessage, _ = common.ConstructPayload(NodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, NodeId, key, false) + NewNodeMessage, _ = utils.ConstructPayload(NodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, NodeId, key, false) ConnToLowerNode.Write(NewNodeMessage) } else { - respcommand, _ := common.ConstructPayload(NodeId, "", "COMMAND", "REONLINE", " ", report, 0, NodeId, key, false) + respcommand, _ := utils.ConstructPayload(NodeId, "", "COMMAND", "REONLINE", " ", report, 0, NodeId, key, false) ConnToLowerNode.Write(respcommand) } case "INIT": - NewNodeMessage, _ = common.ConstructPayload(common.AdminId, "", "COMMAND", "NEW", " ", ConnToLowerNode.RemoteAddr().String(), 0, NodeId, key, false) - NodeInfo.LowerNode.Payload[common.AdminId] = ConnToLowerNode + NewNodeMessage, _ = utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NEW", " ", ConnToLowerNode.RemoteAddr().String(), 0, NodeId, key, false) + NodeInfo.LowerNode.Payload[utils.AdminId] = ConnToLowerNode NodeStuff.ControlConnForLowerNodeChan <- ConnToLowerNode NodeStuff.NewNodeMessageChan <- NewNodeMessage NodeStuff.IsAdmin <- false diff --git a/node/reuse.go b/node/reuse.go index 8598bb6..a5e1fa8 100644 --- a/node/reuse.go +++ b/node/reuse.go @@ -1,8 +1,8 @@ package node import ( - "Stowaway/common" "Stowaway/config" + "Stowaway/utils" "errors" "fmt" "io" @@ -29,12 +29,12 @@ func StartNodeConnReuse(monitor string, listenPort string, nodeID string, key [] continue } - helloMess, _ := common.ConstructPayload(nodeID, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, common.AdminId, key, false) + helloMess, _ := utils.ConstructPayload(nodeID, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, utils.AdminId, key, false) controlConnToUpperNode.Write(helloMess) - common.ExtractPayload(controlConnToUpperNode, key, common.AdminId, true) + utils.ExtractPayload(controlConnToUpperNode, key, utils.AdminId, true) - respcommand, _ := common.ConstructPayload(nodeID, "", "COMMAND", "INIT", " ", listenPort, 0, common.AdminId, key, false) //主动向上级节点发送初始信息 + respcommand, _ := utils.ConstructPayload(nodeID, "", "COMMAND", "INIT", " ", listenPort, 0, utils.AdminId, key, false) //主动向上级节点发送初始信息 _, err = controlConnToUpperNode.Write(respcommand) if err != nil { log.Printf("[*]Error occured: %s", err) @@ -42,7 +42,7 @@ func StartNodeConnReuse(monitor string, listenPort string, nodeID string, key [] } //等待admin为其分配一个id号 for { - command, _ := common.ExtractPayload(controlConnToUpperNode, key, common.AdminId, true) + command, _ := utils.ExtractPayload(controlConnToUpperNode, key, utils.AdminId, true) switch command.Command { case "ID": nodeID = command.NodeId @@ -67,11 +67,11 @@ func ConnectNextNodeReuse(target string, nodeid string, key []byte) bool { continue } - helloMess, _ := common.ConstructPayload(nodeid, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, common.AdminId, key, false) + helloMess, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "STOWAWAYAGENT", " ", " ", 0, utils.AdminId, key, false) controlConnToNextNode.Write(helloMess) for { - command, err := common.ExtractPayload(controlConnToNextNode, key, common.AdminId, true) + command, err := utils.ExtractPayload(controlConnToNextNode, key, utils.AdminId, true) if err != nil { log.Println("[*]", err) return false @@ -79,8 +79,8 @@ func ConnectNextNodeReuse(target string, nodeid string, key []byte) bool { switch command.Command { case "INIT": //类似与上面 - NewNodeMessage, _ := common.ConstructPayload(common.AdminId, "", "COMMAND", "NEW", " ", controlConnToNextNode.RemoteAddr().String(), 0, nodeid, key, false) - NodeInfo.LowerNode.Payload[common.AdminId] = controlConnToNextNode + NewNodeMessage, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NEW", " ", controlConnToNextNode.RemoteAddr().String(), 0, nodeid, key, false) + NodeInfo.LowerNode.Payload[utils.AdminId] = controlConnToNextNode NodeStuff.ControlConnForLowerNodeChan <- controlConnToNextNode NodeStuff.NewNodeMessageChan <- NewNodeMessage NodeStuff.IsAdmin <- false @@ -90,7 +90,7 @@ func ConnectNextNodeReuse(target string, nodeid string, key []byte) bool { NodeStuff.ReOnlineId <- command.CurrentId NodeStuff.ReOnlineConn <- controlConnToNextNode <-NodeStuff.PrepareForReOnlineNodeReady - NewNodeMessage, _ := common.ConstructPayload(nodeid, "", "COMMAND", "REONLINESUC", " ", " ", 0, nodeid, key, false) + NewNodeMessage, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "REONLINESUC", " ", " ", 0, nodeid, key, false) controlConnToNextNode.Write(NewNodeMessage) return true } diff --git a/node/soreuse_reuse.go b/node/soreuse_reuse.go index a1a845a..ddd842e 100644 --- a/node/soreuse_reuse.go +++ b/node/soreuse_reuse.go @@ -1,7 +1,7 @@ package node import ( - "Stowaway/common" + "Stowaway/utils" "fmt" "log" "net" @@ -41,10 +41,10 @@ func StartNodeListenReuse(rehost, report string, NodeId string, key []byte) { } for i := 0; i < 2; i++ { - command, _ := common.ExtractPayload(ConnToLowerNode, key, common.AdminId, true) + command, _ := utils.ExtractPayload(ConnToLowerNode, key, utils.AdminId, true) switch command.Command { case "STOWAWAYADMIN": - respcommand, _ := common.ConstructPayload(NodeId, "", "COMMAND", "INIT", " ", report, 0, common.AdminId, key, false) + respcommand, _ := utils.ConstructPayload(NodeId, "", "COMMAND", "INIT", " ", report, 0, utils.AdminId, key, false) ConnToLowerNode.Write(respcommand) case "ID": NodeStuff.ControlConnForLowerNodeChan <- ConnToLowerNode @@ -54,16 +54,16 @@ func StartNodeListenReuse(rehost, report string, NodeId string, key []byte) { NodeStuff.Adminconn <- ConnToLowerNode case "STOWAWAYAGENT": if !NodeStuff.Offline { - NewNodeMessage, _ = common.ConstructPayload(NodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, NodeId, key, false) + NewNodeMessage, _ = utils.ConstructPayload(NodeId, "", "COMMAND", "CONFIRM", " ", " ", 0, NodeId, key, false) ConnToLowerNode.Write(NewNodeMessage) } else { - respcommand, _ := common.ConstructPayload(NodeId, "", "COMMAND", "REONLINE", " ", report, 0, NodeId, key, false) + respcommand, _ := utils.ConstructPayload(NodeId, "", "COMMAND", "REONLINE", " ", report, 0, NodeId, key, false) ConnToLowerNode.Write(respcommand) } case "INIT": //告知admin新节点消息 - NewNodeMessage, _ = common.ConstructPayload(common.AdminId, "", "COMMAND", "NEW", " ", ConnToLowerNode.RemoteAddr().String(), 0, NodeId, key, false) - NodeInfo.LowerNode.Payload[common.AdminId] = ConnToLowerNode //将这个socket用0号位暂存,等待admin分配完id后再将其放入对应的位置 + NewNodeMessage, _ = utils.ConstructPayload(utils.AdminId, "", "COMMAND", "NEW", " ", ConnToLowerNode.RemoteAddr().String(), 0, NodeId, key, false) + NodeInfo.LowerNode.Payload[utils.AdminId] = ConnToLowerNode //将这个socket用0号位暂存,等待admin分配完id后再将其放入对应的位置 NodeStuff.ControlConnForLowerNodeChan <- ConnToLowerNode NodeStuff.NewNodeMessageChan <- NewNodeMessage //被连接后不终止监听,继续等待可能的后续节点连接,以此组成树状结构 NodeStuff.IsAdmin <- false @@ -93,12 +93,12 @@ func AcceptConnFromUpperNodeReuse(rehost, report string, nodeid string, key []by continue } - common.ExtractPayload(Comingconn, key, common.AdminId, true) + utils.ExtractPayload(Comingconn, key, utils.AdminId, true) - respcommand, _ := common.ConstructPayload(nodeid, "", "COMMAND", "INIT", " ", report, 0, common.AdminId, key, false) + respcommand, _ := utils.ConstructPayload(nodeid, "", "COMMAND", "INIT", " ", report, 0, utils.AdminId, key, false) Comingconn.Write(respcommand) - command, _ := common.ExtractPayload(Comingconn, key, common.AdminId, true) //等待分配id + command, _ := utils.ExtractPayload(Comingconn, key, utils.AdminId, true) //等待分配id if command.Command == "ID" { nodeid = command.NodeId WaitingForConn.Close() diff --git a/common/file.go b/share/file.go similarity index 68% rename from common/file.go rename to share/file.go index 03c8cee..f7ad21c 100644 --- a/common/file.go +++ b/share/file.go @@ -1,6 +1,7 @@ -package common +package share import ( + "Stowaway/utils" "fmt" "math" "net" @@ -12,11 +13,11 @@ import ( "github.com/cheggaaa/pb/v3" ) -var File *FileStatus +var File *utils.FileStatus var Bar *pb.ProgressBar func init() { - File = NewFileStatus() + File = utils.NewFileStatus() } /*-------------------------上传/下载文件相关代码--------------------------*/ @@ -29,13 +30,13 @@ func UploadFile(route, filename string, controlConn *net.Conn, nodeid string, ge if Notagent { fmt.Println("[*]File not found!") } else { - respData, _ := ConstructPayload(nodeid, route, "COMMAND", "FILENOTEXIST", " ", filename, 0, currentid, AESKey, false) //发送文件是否存在的情况 + respData, _ := utils.ConstructPayload(nodeid, route, "COMMAND", "FILENOTEXIST", " ", filename, 0, currentid, AESKey, false) //发送文件是否存在的情况 (*controlConn).Write(respData) } return } - respData, _ := ConstructPayload(nodeid, route, "COMMAND", "FILENAME", " ", info.Name(), 0, currentid, AESKey, false) //发送文件名 + respData, _ := utils.ConstructPayload(nodeid, route, "COMMAND", "FILENAME", " ", info.Name(), 0, currentid, AESKey, false) //发送文件名 (*controlConn).Write(respData) if <-getName { @@ -48,7 +49,7 @@ func UploadFile(route, filename string, controlConn *net.Conn, nodeid string, ge if Notagent { fmt.Println("[*]Cannot read the file") } - respData, _ := ConstructPayload(nodeid, route, "COMMAND", "CANNOTREAD", " ", info.Name(), 0, currentid, AESKey, false) //检查是否能读 + respData, _ := utils.ConstructPayload(nodeid, route, "COMMAND", "CANNOTREAD", " ", info.Name(), 0, currentid, AESKey, false) //检查是否能读 (*controlConn).Write(respData) return } @@ -56,19 +57,19 @@ func UploadFile(route, filename string, controlConn *net.Conn, nodeid string, ge fileSliceNum := math.Ceil(float64(fileInfo.Size()) / 10240) fileSliceStr := strconv.FormatInt(int64(fileSliceNum), 10) //计算文件需要被分多少包 - respData, _ = ConstructPayload(nodeid, route, "COMMAND", "FILESLICENUM", " ", fileSliceStr, 0, currentid, AESKey, false) + respData, _ = utils.ConstructPayload(nodeid, route, "COMMAND", "FILESLICENUM", " ", fileSliceStr, 0, currentid, AESKey, false) (*controlConn).Write(respData) //告知包数量 if Notagent { fmt.Println("\n[*]File transmitting, please wait...") - Bar = NewBar(fileInfo.Size()) + Bar = utils.NewBar(fileInfo.Size()) Bar.Start() } <-File.TotalConfirm //当对端确定接收到包数量通知后继续 filesize := strconv.FormatInt(fileInfo.Size(), 10) - respData, _ = ConstructPayload(nodeid, route, "COMMAND", "FILESIZE", " ", filesize, 0, currentid, AESKey, false) + respData, _ = utils.ConstructPayload(nodeid, route, "COMMAND", "FILESIZE", " ", filesize, 0, currentid, AESKey, false) (*controlConn).Write(respData) //告知文件大小 <-File.TotalConfirm //当对端确定接收到文件大小通知后继续 @@ -81,7 +82,7 @@ func UploadFile(route, filename string, controlConn *net.Conn, nodeid string, ge } return } - fileData, _ := ConstructPayload(nodeid, route, "DATA", "FILEDATA", strconv.Itoa(slicenum), string(buff[:n]), 0, currentid, AESKey, false) //发送文件内容 + fileData, _ := utils.ConstructPayload(nodeid, route, "DATA", "FILEDATA", strconv.Itoa(slicenum), string(buff[:n]), 0, currentid, AESKey, false) //发送文件内容 (*controlConn).Write(fileData) slicenum++ if Notagent { @@ -90,7 +91,7 @@ func UploadFile(route, filename string, controlConn *net.Conn, nodeid string, ge } } else { if !Notagent { - respData, _ = ConstructPayload(AdminId, route, "COMMAND", "CANNOTUPLOAD", " ", info.Name(), 0, currentid, AESKey, false) //对端没有拿到文件名 + respData, _ = utils.ConstructPayload(utils.AdminId, route, "COMMAND", "CANNOTUPLOAD", " ", info.Name(), 0, currentid, AESKey, false) //对端没有拿到文件名 (*controlConn).Write(respData) } else { fmt.Println("[*]File cannot be uploaded!") @@ -101,7 +102,7 @@ func UploadFile(route, filename string, controlConn *net.Conn, nodeid string, ge //admin下载文件 func DownloadFile(route, filename string, conn net.Conn, nodeid string, currentid string, AESKey []byte) { - respData, _ := ConstructPayload(nodeid, route, "COMMAND", "DOWNLOADFILE", " ", filename, 0, currentid, AESKey, false) + respData, _ := utils.ConstructPayload(nodeid, route, "COMMAND", "DOWNLOADFILE", " ", filename, 0, currentid, AESKey, false) _, err := conn.Write(respData) if err != nil { return @@ -109,7 +110,7 @@ func DownloadFile(route, filename string, conn net.Conn, nodeid string, currenti } //admin || agent接收文件 -func ReceiveFile(route string, controlConnToAdmin *net.Conn, FileDataMap *IntStrMap, CannotRead chan bool, UploadFile *os.File, AESKey []byte, Notagent bool, currentid string) { +func ReceiveFile(route string, controlConnToAdmin *net.Conn, FileDataMap *utils.IntStrMap, CannotRead chan bool, UploadFile *os.File, AESKey []byte, Notagent bool, currentid string) { defer UploadFile.Close() if Notagent { fmt.Println("\n[*]Downloading file,please wait......") @@ -119,7 +120,7 @@ func ReceiveFile(route string, controlConnToAdmin *net.Conn, FileDataMap *IntStr <-File.ReceiveFileSize //确认收到文件大小 if Notagent { - Bar = NewBar(File.FileSize) + Bar = utils.NewBar(File.FileSize) Bar.Start() } @@ -152,7 +153,7 @@ func ReceiveFile(route string, controlConnToAdmin *net.Conn, FileDataMap *IntStr Bar.Finish() fmt.Println("[*]Transmission complete") } else { - respData, _ := ConstructPayload(AdminId, route, "COMMAND", "TRANSSUCCESS", " ", " ", 0, currentid, AESKey, false) + respData, _ := utils.ConstructPayload(utils.AdminId, route, "COMMAND", "TRANSSUCCESS", " ", " ", 0, currentid, AESKey, false) (*controlConnToAdmin).Write(respData) } return diff --git a/common/heartbeat.go b/share/heartbeat.go similarity index 55% rename from common/heartbeat.go rename to share/heartbeat.go index 7e277d8..5e262eb 100644 --- a/common/heartbeat.go +++ b/share/heartbeat.go @@ -1,13 +1,14 @@ -package common +package share import ( + "Stowaway/utils" "net" "time" ) /*-------------------------心跳包相关代码--------------------------*/ func SendHeartBeatControl(controlConnToUpperNode *net.Conn, nodeid string, key []byte) { - hbcommpack, _ := ConstructPayload(AdminId, "", "COMMAND", "HEARTBEAT", " ", " ", 0, nodeid, key, false) //这里nodeid写adminid,但实际上并不会被传递到admin端,会在HandleConnFromLowerNode中被解析 + hbcommpack, _ := utils.ConstructPayload(utils.AdminId, "", "COMMAND", "HEARTBEAT", " ", " ", 0, nodeid, key, false) //这里nodeid写adminid,但实际上并不会被传递到admin端,会在HandleConnFromLowerNode中被解析 for { time.Sleep(30 * time.Second) _, err := (*controlConnToUpperNode).Write(hbcommpack) diff --git a/common/payload.go b/utils/payload.go similarity index 99% rename from common/payload.go rename to utils/payload.go index f35c05f..c0fbeb0 100644 --- a/common/payload.go +++ b/utils/payload.go @@ -1,4 +1,4 @@ -package common +package utils import ( "Stowaway/config" diff --git a/common/utils.go b/utils/utils.go similarity index 99% rename from common/utils.go rename to utils/utils.go index 893474c..ff3b98b 100644 --- a/common/utils.go +++ b/utils/utils.go @@ -1,4 +1,4 @@ -package common +package utils import ( "crypto/md5"