Skip to content

Commit

Permalink
feat: gob => msgpack + Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiMoebius committed Aug 9, 2024
1 parent 46c6413 commit 7d3588a
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 236 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*
setuptun.sh
dist/
ligolo-ng.history
ligolo-selfcerts/
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export GO111MODULE=on

VERSION=$(shell date +"%Y.%m.%d")

BUILD=$(shell git rev-parse HEAD)
BASEDIR=./dist

LDFLAGS=-ldflags "-s -w -X main.build=${BUILD} -buildid=${BUILD}"
GCFLAGS=-gcflags=all=-trimpath=$(shell echo ${HOME})
ASMFLAGS=-asmflags=all=-trimpath=$(shell echo ${HOME})

GOFILES=`go list -buildvcs=false ./...`
GOFILESNOTEST=`go list -buildvcs=false ./... | grep -v test`

# Make Directory to store executables
$(shell mkdir -p ${BASEDIR})

# goreleaser build --config .goreleaser.yml --rm-dist --skip-validate
all: linux windows
@chmod +x dist/*

linux: lint
@env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath ${LDFLAGS} ${GCFLAGS} ${ASMFLAGS} -o ${BASEDIR}/ligolo-ng-proxy-linux_amd64 cmd/proxy/main.go
@env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath ${LDFLAGS} ${GCFLAGS} ${ASMFLAGS} -o ${BASEDIR}/ligolo-ng-agent-linux_amd64 cmd/agent/main.go

windows: lint
@env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -trimpath ${LDFLAGS} ${GCFLAGS} ${ASMFLAGS} -o ${BASEDIR}/ligolo-ng-proxy-windows_amd64.exe cmd/proxy/main.go
@env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -trimpath ${LDFLAGS} ${GCFLAGS} ${ASMFLAGS} -o ${BASEDIR}/ligolo-ng-agent-windows_amd64.exe cmd/agent/main.go

tidy:
@go mod tidy

update: tidy
@go get -v -d ./...
@go get -u all

dep: ## Get the dependencies
@go install github.com/goreleaser/goreleaser
@go install github.com/securego/gosec/v2/cmd/gosec@latest

lint: ## Lint the files
@env CGO_ENABLED=0 go fmt ${GOFILES}
@env CGO_ENABLED=0 go vet ${GOFILESNOTEST}

security:
@gosec -tests ./...

release:
@goreleaser release --config .github/goreleaser.yml

clean:
@rm -rf ${BASEDIR}

terminal_proxy:
go run cmd/proxy/main.go -selfcert

terminal_agent:
go run cmd/agent/main.go -connect localhost:11601 -ignore-cert


.PHONY: all linux windows tidy update dep lint security release clean terminal
17 changes: 9 additions & 8 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"encoding/hex"
"flag"
"fmt"
"github.com/hashicorp/yamux"
"github.com/nicocha30/ligolo-ng/pkg/agent"
"github.com/nicocha30/ligolo-ng/pkg/utils/selfcert"
"github.com/sirupsen/logrus"
goproxy "golang.org/x/net/proxy"
"net"
"net/http"
"net/url"
"nhooyr.io/websocket"
"os"
"strings"
"time"

"github.com/hashicorp/yamux"
"github.com/nicocha30/ligolo-ng/pkg/agent"
"github.com/nicocha30/ligolo-ng/pkg/utils/selfcert"
"github.com/sirupsen/logrus"
goproxy "golang.org/x/net/proxy"
"nhooyr.io/websocket"
)

var (
Expand All @@ -46,8 +47,8 @@ func main() {
flag.Usage = func() {
fmt.Printf("Ligolo-ng %s / %s / %s\n", version, commit, date)
fmt.Println("Made in France with love by @Nicocha30!")
fmt.Println("https://github.com/nicocha30/ligolo-ng\n")
fmt.Printf("Usage of %s:\n", os.Args[0])
fmt.Println("https://github.com/nicocha30/ligolo-ng")
fmt.Printf("\nUsage of %s:\n", os.Args[0])
flag.PrintDefaults()
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/proxy/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"crypto/tls"
"errors"
"fmt"
"net"
"strconv"
"strings"
"sync"

"github.com/AlecAivazis/survey/v2"
"github.com/desertbit/grumble"
"github.com/hashicorp/yamux"
Expand All @@ -15,10 +20,6 @@ import (
"github.com/nicocha30/ligolo-ng/pkg/proxy"
"github.com/nicocha30/ligolo-ng/pkg/proxy/netstack"
"github.com/sirupsen/logrus"
"net"
"strconv"
"strings"
"sync"
)

var AgentList map[int]*controller.LigoloAgent
Expand Down Expand Up @@ -50,7 +51,7 @@ func RegisterAgent(agent *controller.LigoloAgent) error {
}

for lid, listener := range registeredAgents.Listeners {
logrus.Info("Restarting listener: %s", listener.String())
logrus.Info(fmt.Sprintf("Restarting listener: %s", listener.String()))
if err := listener.ResetMultiplexer(registeredAgents.Session); err != nil {
logrus.Errorf("Failed to reset yamux: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func main() {
flag.Usage = func() {
fmt.Printf("Ligolo-ng %s / %s / %s\n", version, commit, date)
fmt.Println("Made in France with love by @Nicocha30!")
fmt.Println("https://github.com/nicocha30/ligolo-ng\n")
fmt.Printf("Usage of %s:\n", os.Args[0])
fmt.Println("https://github.com/nicocha30/ligolo-ng")
fmt.Printf("\nUsage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
Expand All @@ -62,8 +62,8 @@ func main() {
a.Println(" / / / / __ `/ __ \\/ / __ \\______/ __ \\/ __ `/")
a.Println(" / /___/ / /_/ / /_/ / / /_/ /_____/ / / / /_/ / ")
a.Println("/_____/_/\\__, /\\____/_/\\____/ /_/ /_/\\__, / ")
a.Println(" /____/ /____/ \n")
a.Println(" Made in France ♥ by @Nicocha30!")
a.Println(" /____/ /____/ ")
a.Println("\n Made in France ♥ by @Nicocha30!")
a.Printf(" Version: %s\n\n", version)
})

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/google/uuid v1.3.0
github.com/nicocha30/gvisor-ligolo v0.0.0-20230726075806-989fa2c0a413
github.com/vishvananda/netlink v1.1.1-0.20211118161826-650dca95af54
github.com/vmihailenco/msgpack/v5 v5.4.1
golang.org/x/sys v0.21.0
nhooyr.io/websocket v1.8.11
)
Expand All @@ -39,6 +40,7 @@ require (
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ github.com/vishvananda/netlink v1.1.1-0.20211118161826-650dca95af54 h1:8mhqcHPqT
github.com/vishvananda/netlink v1.1.1-0.20211118161826-650dca95af54/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
81 changes: 26 additions & 55 deletions pkg/agent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"net"
"os"
"os/user"
"syscall"
"time"

"github.com/google/uuid"

"github.com/nicocha30/ligolo-ng/pkg/agent/neterror"
"github.com/nicocha30/ligolo-ng/pkg/agent/smartping"
"github.com/nicocha30/ligolo-ng/pkg/protocol"
Expand Down Expand Up @@ -89,11 +90,11 @@ func HandleConn(conn net.Conn) {
return
}

e := decoder.Envelope.Payload
switch decoder.Envelope.Type {
e := decoder
switch decoder.Payload.(type) {

case protocol.MessageConnectRequest:
connRequest := e.(protocol.ConnectRequestPacket)
case *protocol.ConnectRequestPacket:
connRequest := e.Payload.(*protocol.ConnectRequestPacket)
encoder := protocol.NewEncoder(conn)

logrus.Debugf("Got connect request to %s:%d", connRequest.Address, connRequest.Port)
Expand Down Expand Up @@ -129,30 +130,24 @@ func HandleConn(conn net.Conn) {
} else {
connectPacket.Established = true
}
if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageConnectResponse,
Payload: connectPacket,
}); err != nil {
if err := encoder.Encode(connectPacket); err != nil {
logrus.Error(err)
return
}
if connectPacket.Established {
relay.StartRelay(targetConn, conn)
}
case protocol.MessageHostPingRequest:
pingRequest := e.(protocol.HostPingRequestPacket)
case *protocol.HostPingRequestPacket:
pingRequest := e.Payload.(*protocol.HostPingRequestPacket)
encoder := protocol.NewEncoder(conn)

pingResponse := protocol.HostPingResponsePacket{Alive: smartping.TryResolve(pingRequest.Address)}

if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageHostPingResponse,
Payload: pingResponse,
}); err != nil {
if err := encoder.Encode(pingResponse); err != nil {
logrus.Error(err)
return
}
case protocol.MessageInfoRequest:
case *protocol.InfoRequestPacket:
var username string
encoder := protocol.NewEncoder(conn)
hostname, err := os.Hostname()
Expand All @@ -178,16 +173,13 @@ func HandleConn(conn net.Conn) {
SessionID: sessionID,
}

if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageInfoReply,
Payload: infoResponse,
}); err != nil {
if err := encoder.Encode(infoResponse); err != nil {
logrus.Error(err)
return
}
case protocol.MessageListenerCloseRequest:
case *protocol.ListenerCloseRequestPacket:
// Request to close a listener
closeRequest := e.(protocol.ListenerCloseRequestPacket)
closeRequest := e.Payload.(*protocol.ListenerCloseRequestPacket)
encoder := protocol.NewEncoder(conn)

var err error
Expand All @@ -209,15 +201,12 @@ func HandleConn(conn net.Conn) {
listenerResponse.ErrString = err.Error()
}

if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerCloseResponse,
Payload: listenerResponse,
}); err != nil {
if err := encoder.Encode(listenerResponse); err != nil {
logrus.Error(err)
}

case protocol.MessageListenerRequest:
listenRequest := e.(protocol.ListenerRequestPacket)
case *protocol.ListenerRequestPacket:
listenRequest := e.Payload.(*protocol.ListenerRequestPacket)
encoder := protocol.NewEncoder(conn)
connTrackChan := make(chan int32)
stopChan := make(chan error)
Expand All @@ -230,10 +219,7 @@ func HandleConn(conn net.Conn) {
Err: true,
ErrString: err.Error(),
}
if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerResponse,
Payload: listenerResponse,
}); err != nil {
if err := encoder.Encode(listenerResponse); err != nil {
logrus.Error(err)
}
return
Expand All @@ -244,10 +230,7 @@ func HandleConn(conn net.Conn) {
Err: false,
ErrString: "",
}
if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerResponse,
Payload: listenerResponse,
}); err != nil {
if err := encoder.Encode(listenerResponse); err != nil {
logrus.Error(err)
}
go func() {
Expand All @@ -265,10 +248,7 @@ func HandleConn(conn net.Conn) {
Err: true,
ErrString: err.Error(),
}
if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerResponse,
Payload: listenerResponse,
}); err != nil {
if err := encoder.Encode(listenerResponse); err != nil {
logrus.Error(err)
}
return
Expand All @@ -279,10 +259,7 @@ func HandleConn(conn net.Conn) {
Err: false,
ErrString: "",
}
if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerResponse,
Payload: listenerResponse,
}); err != nil {
if err := encoder.Encode(listenerResponse); err != nil {
logrus.Error(err)
}
go relay.StartRelay(conn, udplistener)
Expand All @@ -307,10 +284,7 @@ func HandleConn(conn net.Conn) {
}
}

if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerBindResponse,
Payload: bindResponse,
}); err != nil {
if err := encoder.Encode(bindResponse); err != nil {
logrus.Error(err)
}

Expand All @@ -320,8 +294,8 @@ func HandleConn(conn net.Conn) {

}
}
case protocol.MessageListenerSockRequest:
sockRequest := e.(protocol.ListenerSockRequestPacket)
case *protocol.ListenerSockRequestPacket:
sockRequest := e.Payload.(*protocol.ListenerSockRequestPacket)
encoder := protocol.NewEncoder(conn)

var sockResponse protocol.ListenerSockResponsePacket
Expand All @@ -331,10 +305,7 @@ func HandleConn(conn net.Conn) {
sockResponse.Err = true
}

if err := encoder.Encode(protocol.Envelope{
Type: protocol.MessageListenerSockResponse,
Payload: sockResponse,
}); err != nil {
if err := encoder.Encode(sockResponse); err != nil {
logrus.Error(err)
return
}
Expand All @@ -346,7 +317,7 @@ func HandleConn(conn net.Conn) {
netConn := listenerConntrack[sockRequest.SockID]
relay.StartRelay(netConn, conn)

case protocol.MessageClose:
case *protocol.ListenerCloseResponsePacket:
os.Exit(0)

}
Expand Down
Loading

0 comments on commit 7d3588a

Please sign in to comment.