Skip to content

Commit

Permalink
Sort through codes structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4ntonn committed Apr 22, 2020
1 parent ed4104a commit 900ca63
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 336 deletions.
83 changes: 42 additions & 41 deletions admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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": //接收文件内容
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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()
Expand All @@ -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!")
Expand Down
6 changes: 3 additions & 3 deletions admin/command.go
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
48 changes: 24 additions & 24 deletions admin/function.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package admin

import (
"Stowaway/common"
"Stowaway/utils"
"bufio"
"fmt"
"io/ioutil"
Expand All @@ -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)
}

Expand Down Expand Up @@ -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!")
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}()
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 900ca63

Please sign in to comment.