From 0575ec97a53f1f7e62d673f32411ea5279577335 Mon Sep 17 00:00:00 2001 From: imterah Date: Sun, 12 Jan 2025 11:36:43 -0500 Subject: [PATCH] chore: Adds more Go support. --- .gitignore | 2 +- data.capnp | 40 +++++++++++++++++++-------------------- iptool.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 iptool.go diff --git a/.gitignore b/.gitignore index 7a94e31..892655b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -*.go +*.capnp.go *.rs diff --git a/data.capnp b/data.capnp index 12ee199..576835f 100644 --- a/data.capnp +++ b/data.capnp @@ -14,6 +14,11 @@ enum StatusCode { failure @1; } +enum InResponseTo { + checkClientParameters @0; + checkServerParameters @1; +} + struct Start { arguments @0 :Data; } @@ -49,17 +54,17 @@ struct ProxyStatusResponse { isActive @4 :Bool; } +struct ProxyInstance { + sourceIP @0 :Text; + sourcePort @1 :UInt16; + destPort @2 :UInt16; + protocol @3 :Protocol; +} + struct ProxyInstanceRequest {} struct ProxyInstanceResponse { proxies @0 :List(ProxyInstance); - - struct ProxyInstance { - sourceIP @0 :Text; - sourcePort @1 :UInt16; - destPort @2 :UInt16; - protocol @3 :Protocol; - } } struct BackendStatusRequest {} @@ -70,18 +75,18 @@ struct BackendStatusResponse { message @2 :Text; } +struct Connection { + sourceIP @0 :Data; + sourcePort @1 :UInt16; + destPort @2 :UInt16; + clientIP @3 :Data; + clientPort @4 :UInt16; +} + struct ProxyConnectionsRequest {} struct ProxyConnectionsResponse { connections @0 :List(Connection); - - struct Connection { - sourceIP @0 :Data; - sourcePort @1 :UInt16; - destPort @2 :UInt16; - clientIP @3 :Data; - clientPort @4 :UInt16; - } } struct CheckClientParameters { @@ -99,9 +104,4 @@ struct CheckParametersResponse { inResponseTo @0 :InResponseTo; isValid @1 :Bool; message @2 :Text; - - enum InResponseTo { - checkClientParameters @0; - checkServerParameters @1; - } } diff --git a/iptool.go b/iptool.go new file mode 100644 index 0000000..a90fa61 --- /dev/null +++ b/iptool.go @@ -0,0 +1,55 @@ +package commonbackend + +import ( + "fmt" + "net" +) + +func IPStringToIPBytes(ip string) ([]byte, error) { + sourceIP := net.ParseIP(ip) + ipBytes := sourceIP.To4() + + if ipBytes == nil { + ipBytes = sourceIP.To16() + + if ipBytes == nil { + return nil, fmt.Errorf("invalid IP address recieved") + } + } + + return ipBytes, nil +} + +func IPBytesToIPSafe(ip []byte) (net.IP, error) { + sourceIP := net.IP(ip) + ipBytes := sourceIP.To4() + + if ipBytes != nil { + return ipBytes, nil + } + + ipBytes = sourceIP.To16() + + if ipBytes != nil { + return ipBytes, nil + } + + return nil, fmt.Errorf("invalid IP address recieved") +} + +func IPBytesToString(ip []byte) (string, error) { + sourceIP := net.IP(ip) + ipBytes := sourceIP.To4() + + if ipBytes != nil { + return ipBytes.String(), nil + } + + ipBytes = sourceIP.To16() + + if ipBytes != nil { + return ipBytes.String(), nil + } + + return "", fmt.Errorf("invalid IP address recieved") +}