Skip to content

Commit

Permalink
feat: Add support for statically linking the main binary, use Flatpak…
Browse files Browse the repository at this point in the history
…-support libraries out of the box

Signed-off-by: Felicitas Pojtinger <[email protected]>
  • Loading branch information
pojntfx committed Sep 7, 2024
1 parent 7f2d84c commit ee8edb1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 121 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ All arguments passed to the binary will be forwarded to the browser used to disp
## Acknowledgements

- [pojntfx/hydrapp](https://github.com/pojntfx/hydrapp) provides the application framework.
- [google/gopacket](https://github.com/google/gopacket) provides the library that the packets are being parsed with.
- [gopacket/gopacket](https://github.com/gopacket/gopacket) provides the library that the packets are being parsed with.
- [oschwald/geoip2-golang](https://github.com/oschwald/geoip2-golang) provides the library for reading the GeoIP2 database.

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion com.pojtinger.felicitas.connmapper.main.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"build-commands": [
"git config --global --add safe.directory '*'",
"GOPROXY='https://proxy.golang.org,direct' GOFLAGS=\"-x {{ .GoFlags }}\" sh -c '. /usr/lib/sdk/golang/enable.sh && . /usr/lib/sdk/node20/enable.sh && {{ .GoGenerate }}'",
". /usr/lib/sdk/golang/enable.sh && export GOPROXY='https://proxy.golang.org,direct' GOFLAGS='-x {{ .GoFlags }}' && export CGO_LDFLAGS='-L/app/lib' && go build -o out/{{ .AppID }} {{ .GoMain }}",
". /usr/lib/sdk/golang/enable.sh && export GOPROXY='https://proxy.golang.org,direct' GOFLAGS='-x {{ .GoFlags }}' && export CGO_LDFLAGS='-L/app/lib' && export CGO_ENABLED=0 && go build -tags 'flatpak' -o out/{{ .AppID }} {{ .GoMain }}",
"install -D out/{{ .AppID }} /app/bin/{{ .AppID }}",
"for icon in 16x16 22x22 24x24 32x32 36x36 48x48 64x64 72x72 96x96 128x128 192x192 256x256 512x512; do cp {{ .GoMain }}/icon-${icon}.png out/icon-${icon}.png; done",
"desktop-file-install --dir=/app/share/applications {{ .GoMain }}/{{ .AppID }}.desktop",
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.1

require (
github.com/cli/browser v1.3.0
github.com/google/gopacket v1.1.19
github.com/gopacket/gopacket v1.2.0
github.com/oschwald/geoip2-golang v1.11.0
github.com/pojntfx/hydrapp/hydrapp v0.0.0-20240827002308-22c8dc1ea9b8
github.com/pojntfx/panrpc/go v0.0.0-20240816011753-7169be8c89fb
Expand Down Expand Up @@ -34,6 +34,7 @@ require (
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/image v0.19.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyT
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopacket/gopacket v1.2.0 h1:eXbzFad7f73P1n2EJHQlsKuvIMJjVXK5tXoSca78I3A=
github.com/gopacket/gopacket v1.2.0/go.mod h1:BrAKEy5EOGQ76LSqh7DMAr7z0NNPdczWm2GxCG7+I8M=
github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8=
github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down Expand Up @@ -72,19 +72,15 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand All @@ -97,7 +93,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -123,11 +118,9 @@ golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
33 changes: 2 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"github.com/pojntfx/connmapper/pkg/backend"
"github.com/pojntfx/connmapper/pkg/frontend"
"github.com/pojntfx/connmapper/pkg/utils"
"github.com/pojntfx/hydrapp/hydrapp/pkg/config"
_ "github.com/pojntfx/hydrapp/hydrapp/pkg/fixes"
"github.com/pojntfx/hydrapp/hydrapp/pkg/ui"
Expand All @@ -46,35 +43,9 @@ func main() {
panic(err)
}

handle, err := pcap.OpenLive(flag.Arg(0), int32(mtu), true, pcap.BlockForever)
if err != nil {
// GoPacket doesn't export the permission error, so we need to compare error strings
if strings.HasSuffix(err.Error(), "(socket: Operation not permitted)") {
fmt.Print(backend.TraceCommandHandshakeHandlePermissionDenied)
} else {
fmt.Print(backend.TraceCommandHandshakeHandleUnexpectedError)
}

if err := utils.TraceDevice(mtu, flag.Arg(0)); err != nil {
panic(err)
}
defer handle.Close()

fmt.Print(backend.TraceCommandHandshakeHandleAcquired)

source := gopacket.NewPacketSource(handle, handle.LinkType())

encoder := json.NewEncoder(os.Stdout)
for packet := range source.Packets() {
rawPacket := &backend.Packet{
Data: packet.Data(),
LinkType: handle.LinkType(),
DecodeOptions: source.DecodeOptions,
}

if err := encoder.Encode(rawPacket); err != nil {
panic(err)
}
}

return
}
Expand Down
86 changes: 8 additions & 78 deletions pkg/backend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ import (
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/cli/browser"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/oschwald/geoip2-golang"
uutils "github.com/pojntfx/connmapper/pkg/utils"
"github.com/pojntfx/hydrapp/hydrapp/pkg/ui"
Expand All @@ -44,10 +42,6 @@ const (
TraceCommandEnv = "CONNMAPPER_TRACE"

traceCommandHandshakeLen = 2

TraceCommandHandshakeHandleAcquired = "AQ"
TraceCommandHandshakeHandlePermissionDenied = "PD"
TraceCommandHandshakeHandleUnexpectedError = "UE"
)

func lookupLocation(db *geoip2.Reader, ip net.IP) (
Expand Down Expand Up @@ -101,18 +95,6 @@ func getTracedConnectionID(connection tracedConnection) string {
connection.DstIP + "-"
}

type Device struct {
PcapName string
NetName string
MTU int
}

type Packet struct {
Data []byte `json:"data"`
LinkType layers.LinkType `json:"linkType"`
DecodeOptions gopacket.DecodeOptions `json:"decodeOptions"`
}

type local struct {
connections map[string]tracedConnection
connectionsLock sync.Mutex
Expand Down Expand Up @@ -213,60 +195,8 @@ func (l *local) DownloadDatabase(ctx context.Context, licenseKey string) error {
return nil
}

func (l *local) ListDevices(ctx context.Context) ([]Device, error) {
pcapDevices, err := pcap.FindAllDevs()
if err != nil {
return []Device{}, err
}

netIfaces, err := net.Interfaces()
if err != nil {
return []Device{}, err
}

devices := []Device{}
for _, pcapDevice := range pcapDevices {
pcapAddresses := []string{}
for _, cidr := range pcapDevice.Addresses {
pcapAddresses = append(pcapAddresses, cidr.IP.String())
}

var netIface *net.Interface
for _, candidate := range netIfaces {
rawNetAddresses, err := candidate.Addrs()
if err != nil {
return []Device{}, err
}

netAddresses := []string{}
for _, rawCandidateAddress := range rawNetAddresses {
ip, _, err := net.ParseCIDR(rawCandidateAddress.String())
if err != nil {
return []Device{}, err
}

netAddresses = append(netAddresses, ip.String())
}

if slices.EqualFunc(netAddresses, pcapAddresses, func(a string, b string) bool {
return a == b
}) {
netIface = &candidate

break
}
}

if netIface != nil {
devices = append(devices, Device{
PcapName: pcapDevice.Name,
NetName: netIface.Name,
MTU: netIface.MTU,
})
}
}

return devices, nil
func (l *local) ListDevices(ctx context.Context) ([]uutils.Device, error) {
return uutils.ListDevices(ctx)
}

func (l *local) SetMaxPacketCache(ctx context.Context, packetCache int) error {
Expand Down Expand Up @@ -331,7 +261,7 @@ func (l *local) RestartApp(ctx context.Context) error {
return nil
}

func (l *local) TraceDevice(ctx context.Context, device Device) error {
func (l *local) TraceDevice(ctx context.Context, device uutils.Device) error {
l.tracingDevicesLock.Lock()
defer l.tracingDevicesLock.Unlock()

Expand Down Expand Up @@ -379,10 +309,10 @@ restartTraceCommand:
}

switch string(handshake) {
case TraceCommandHandshakeHandleAcquired:
case uutils.TraceCommandHandshakeHandleAcquired:
break

case TraceCommandHandshakeHandlePermissionDenied:
case uutils.TraceCommandHandshakeHandlePermissionDenied:
if cmd.Process != nil {
_ = cmd.Process.Kill()

Expand Down Expand Up @@ -447,7 +377,7 @@ restartTraceCommand:

decoder := json.NewDecoder(stdout)
for {
var rawPacket Packet
var rawPacket uutils.Packet
if err := decoder.Decode(&rawPacket); err != nil {

log.Println("Could not continue capturing:", err)
Expand Down

0 comments on commit ee8edb1

Please sign in to comment.