aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-08-27 10:28:52 -0400
committerXe Iaso <me@xeiaso.net>2023-08-27 10:28:52 -0400
commit3d0647e946014516df33de0b18d2a16eec835bed (patch)
tree78d0e661b76ed2da4f4c579462856c45477852d8 /cmd
parent5ff0a9652cc7270d3a8aaa119ea00bfd6039eaf9 (diff)
downloadx-3d0647e946014516df33de0b18d2a16eec835bed.tar.xz
x-3d0647e946014516df33de0b18d2a16eec835bed.zip
cmd/sanguisuga: use wireguard to download files over DCC
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/sanguisuga/config.default.ts29
-rw-r--r--cmd/sanguisuga/config.go46
-rw-r--r--cmd/sanguisuga/dcc.go2
-rw-r--r--cmd/sanguisuga/internal/dcc/dcc.go10
-rw-r--r--cmd/sanguisuga/internal/key2hex/key2hex.go16
-rw-r--r--cmd/sanguisuga/main.go27
6 files changed, 123 insertions, 7 deletions
diff --git a/cmd/sanguisuga/config.default.ts b/cmd/sanguisuga/config.default.ts
index cd42ff4..1a1bad2 100644
--- a/cmd/sanguisuga/config.default.ts
+++ b/cmd/sanguisuga/config.default.ts
@@ -17,7 +17,7 @@ export type Transmission = {
host: string;
user: string;
password: string;
- https: bool;
+ https: boolean;
rpcURI: string;
};
@@ -32,6 +32,20 @@ export type Telegram = {
mentionUser: number;
};
+export type WireGuardPeer = {
+ publicKey: string;
+ endpoint: string;
+ allowedIPs: string[];
+};
+
+export type WireGuard = {
+ privateKey: string;
+ address: string[];
+ dns: string;
+ peers: WireGuardPeer[];
+};
+
+
export type Config = {
irc: IRC;
xdcc: IRC;
@@ -41,6 +55,7 @@ export type Config = {
tailscale: Tailscale;
baseDiskPath: string;
telegram: Telegram;
+ wireguard: WireGuard;
};
export default {
@@ -85,4 +100,16 @@ export default {
token: "",
mentionUser: 0,
},
+ wireguard: { // for downloading files over DCC (XDCC)
+ privateKey: "",
+ address: [],
+ dns: "",
+ peers: [
+ {
+ publicKey: "",
+ allowedIPs: [],
+ endpoint: "",
+ },
+ ],
+ },
} satisfies Config;
diff --git a/cmd/sanguisuga/config.go b/cmd/sanguisuga/config.go
index e2e4965..cf98356 100644
--- a/cmd/sanguisuga/config.go
+++ b/cmd/sanguisuga/config.go
@@ -1,6 +1,13 @@
package main
-import "log/slog"
+import (
+ "fmt"
+ "io"
+ "log/slog"
+ "net/netip"
+
+ "within.website/x/cmd/sanguisuga/internal/key2hex"
+)
type IRC struct {
Server string `json:"server"`
@@ -43,6 +50,42 @@ type Telegram struct {
MentionUser int64 `json:"mentionUser"`
}
+type WireGuard struct {
+ PrivateKey string `json:"privateKey"`
+ Address []netip.Addr `json:"address"`
+ DNS netip.Addr `json:"dns"`
+ Peers []WireGuardPeer `json:"peers"`
+}
+
+type WireGuardPeer struct {
+ PublicKey string `json:"publicKey"`
+ AllowedIPs []string `json:"allowedIPs"`
+ Endpoint string `json:"endpoint"`
+}
+
+func (w WireGuard) UAPI(out io.Writer) error {
+ pkey, err := key2hex.Convert(w.PrivateKey)
+ if err != nil {
+ return err
+ }
+ fmt.Fprintf(out, "private_key=%s\n", pkey)
+ fmt.Fprintln(out, "listen_port=0")
+ fmt.Fprintln(out, "replace_peers=true")
+ for _, peer := range w.Peers {
+ pkey, err := key2hex.Convert(peer.PublicKey)
+ if err != nil {
+ return err
+ }
+ fmt.Fprintf(out, "public_key=%s\n", pkey)
+ fmt.Fprintf(out, "endpoint=%s\n", peer.Endpoint)
+ for _, ip := range peer.AllowedIPs {
+ fmt.Fprintf(out, "allowed_ip=%s\n", ip)
+ }
+ fmt.Fprintln(out, "persistent_keepalive_interval=25")
+ }
+ return nil
+}
+
type Config struct {
IRC IRC `json:"irc"`
XDCC IRC `json:"xdcc"`
@@ -52,4 +95,5 @@ type Config struct {
Tailscale Tailscale `json:"tailscale"`
BaseDiskPath string `json:"baseDiskPath"`
Telegram Telegram `json:"telegram"`
+ WireGuard WireGuard `json:"wireguard"`
}
diff --git a/cmd/sanguisuga/dcc.go b/cmd/sanguisuga/dcc.go
index c6ef755..aa141d6 100644
--- a/cmd/sanguisuga/dcc.go
+++ b/cmd/sanguisuga/dcc.go
@@ -334,7 +334,7 @@ waitLoop:
}
defer fout.Close()
- d := dcc.NewDCC(addr, size, fout)
+ d := dcc.NewDCC(addr, size, fout, s.tnet.DialContext)
ctx, cancel := context.WithTimeout(ev.Ctx, 120*time.Minute)
defer cancel()
diff --git a/cmd/sanguisuga/internal/dcc/dcc.go b/cmd/sanguisuga/internal/dcc/dcc.go
index 4975961..82d2ff3 100644
--- a/cmd/sanguisuga/internal/dcc/dcc.go
+++ b/cmd/sanguisuga/internal/dcc/dcc.go
@@ -48,6 +48,9 @@ type DCC struct {
// destination writer
writer io.Writer
+
+ // dial function
+ dialFunc func(ctx context.Context, network, address string) (net.Conn, error)
}
// NewDCC creates a new DCC instance.
@@ -59,6 +62,7 @@ func NewDCC(
address string,
size int,
writer io.Writer,
+ dialFunc func(ctx context.Context, network, address string) (net.Conn, error),
) *DCC {
return &DCC{
address: address,
@@ -66,6 +70,7 @@ func NewDCC(
progressc: make(chan Progress, 1),
done: make(chan error, 1),
writer: writer,
+ dialFunc: dialFunc,
}
}
@@ -169,10 +174,7 @@ func (d *DCC) Run(ctx context.Context) (
// assign the passed context
d.ctx = ctx
- dialer := &net.Dialer{Resolver: net.DefaultResolver}
- conn, err := dialer.DialContext(
- d.ctx, "tcp", d.address,
- )
+ conn, err := d.dialFunc(d.ctx, "tcp", d.address)
if err != nil {
d.done <- err
diff --git a/cmd/sanguisuga/internal/key2hex/key2hex.go b/cmd/sanguisuga/internal/key2hex/key2hex.go
new file mode 100644
index 0000000..b7427a3
--- /dev/null
+++ b/cmd/sanguisuga/internal/key2hex/key2hex.go
@@ -0,0 +1,16 @@
+package key2hex
+
+import (
+ "encoding/base64"
+ "encoding/hex"
+)
+
+func Convert(data string) (string, error) {
+ buf := make([]byte, base64.StdEncoding.DecodedLen(len(data))-1)
+ _, err := base64.StdEncoding.Decode(buf, []byte(data))
+ if err != nil {
+ return "", err
+ }
+
+ return hex.EncodeToString(buf), nil
+}
diff --git a/cmd/sanguisuga/main.go b/cmd/sanguisuga/main.go
index 3dfd2c4..1e48639 100644
--- a/cmd/sanguisuga/main.go
+++ b/cmd/sanguisuga/main.go
@@ -12,6 +12,7 @@ import (
"log"
"log/slog"
"net/http"
+ "net/netip"
"os"
"path/filepath"
"regexp"
@@ -23,6 +24,9 @@ import (
tu "github.com/mymmrac/telego/telegoutil"
irc "github.com/thoj/go-ircevent"
"go.jetpack.io/tyson"
+ "golang.zx2c4.com/wireguard/conn"
+ "golang.zx2c4.com/wireguard/device"
+ "golang.zx2c4.com/wireguard/tun/netstack"
"honnef.co/go/transmission"
"tailscale.com/hostinfo"
"tailscale.com/jsondb"
@@ -174,12 +178,33 @@ func main() {
defer bot.StopLongPolling()
+ tun, tnet, err := netstack.CreateNetTUN(
+ c.WireGuard.Address,
+ []netip.Addr{c.WireGuard.DNS},
+ 1280,
+ )
+ if err != nil {
+ log.Fatalf("can't create tun: %v", err)
+ }
+
+ var confSB bytes.Buffer
+ if err := c.WireGuard.UAPI(&confSB); err != nil {
+ log.Fatalf("can't write wireguard config: %v", err)
+ }
+
+ dev := device.NewDevice(tun, conn.NewStdNetBind(), device.NewLogger(device.LogLevelError, "wireguard: "))
+ if err := dev.IpcSetOperation(&confSB); err != nil {
+ log.Fatalf("can't set wireguard config: %v", err)
+ }
+
s := &Sanguisuga{
Config: c,
cl: cl,
db: db,
bot: bot,
tmpl: template.Must(template.ParseFS(templates, "tmpl/*.html")),
+ tnet: tnet,
+ srv: srv,
animeInFlight: map[string]*SubspleaseAnnouncement{},
}
@@ -226,6 +251,8 @@ type Sanguisuga struct {
dbLock sync.Mutex
bot *telego.Bot
tmpl *template.Template
+ tnet *netstack.Net
+ srv *tsnet.Server
animeInFlight map[string]*SubspleaseAnnouncement
aifLock sync.Mutex