From b0ebab243ea85633762a815647b2c3e4bd2adf08 Mon Sep 17 00:00:00 2001 From: Sleeyax Date: Wed, 4 May 2022 18:51:49 +0200 Subject: [PATCH] Add proxy support (closes #13) --- Dockerfile | 3 ++- README.md | 4 ++++ bot.go | 1 + cmd/main.go | 10 ++++++++++ go.mod | 8 ++++---- go.sum | 18 +++++++++++++----- kubernetes.yaml | 5 +++++ types.go | 12 ++++++++++++ 8 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index b18b7db..e5f51f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,12 @@ FROM golang:1.18-alpine WORKDIR /app -# expose available environment varuables +# expose available environment variables ENV DISCORD_TOKEN="" ENV ATERNOS_SESSION="" ENV ATERNOS_SERVER="" ENV MONGO_DB_URI="" +ENV PROXY="" # install dependencies COPY go.mod ./ diff --git a/README.md b/README.md index 08b9363..ba68537 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,10 @@ Copy and paste your `ATERNOS_SESSION` cookie here. This is only required if you Copy and paste your `ATERNOS_SERVER` cookie here. This is only required if you don't want to use MongoDB. Credentials are stored in memory. +`PROXY` + +HTTP(S) proxy to use for all outgoing connections. This should preferably be a stable rotating proxy when applied in production. + ### Builds diff --git a/bot.go b/bot.go index 55c482e..2a37276 100644 --- a/bot.go +++ b/bot.go @@ -111,6 +111,7 @@ func (ab *Bot) createOptions(guildId string) (*aternos.Options, error) { Value: "en", }, }, + Proxy: ab.Proxy, } settings, err := ab.Database.GetServerSettings(guildId) diff --git a/cmd/main.go b/cmd/main.go index 5709ad2..d04eed9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,6 +5,7 @@ import ( discordBot "github.com/sleeyax/aternos-discord-bot" "github.com/sleeyax/aternos-discord-bot/database" "log" + "net/url" "os" "os/signal" "syscall" @@ -16,6 +17,7 @@ func main() { session := os.Getenv("ATERNOS_SESSION") server := os.Getenv("ATERNOS_SERVER") mongoDbUri := os.Getenv("MONGO_DB_URI") + proxy := os.Getenv("PROXY") // Validate values if token == "" || (mongoDbUri == "" && (session == "" || server == "")) { @@ -32,6 +34,14 @@ func main() { bot.Database = database.NewInMemory(session, server) } + if proxy != "" { + u, err := url.Parse(proxy) + if err != nil { + log.Fatalln(err) + } + bot.Proxy = u + } + if err := bot.Start(); err != nil { log.Fatalln(err) } diff --git a/go.mod b/go.mod index 8919734..9997c99 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.18 require ( github.com/bwmarrin/discordgo v0.24.0 - github.com/sleeyax/aternos-api v0.3.0 + github.com/sleeyax/aternos-api v0.3.4 + go.mongodb.org/mongo-driver v1.9.0 ) require ( @@ -17,7 +18,7 @@ require ( github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect github.com/go-stack/stack v1.8.0 // indirect github.com/golang/snappy v0.0.1 // indirect - github.com/gorilla/websocket v1.4.3-0.20220104015952-9111bb834a68 // indirect + github.com/gorilla/websocket v1.5.0 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/klauspost/compress v1.13.6 // indirect github.com/pkg/errors v0.9.1 // indirect @@ -29,11 +30,10 @@ require ( github.com/xdg-go/scram v1.0.2 // indirect github.com/xdg-go/stringprep v1.0.2 // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect - go.mongodb.org/mongo-driver v1.9.0 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect - golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect golang.org/x/text v0.3.6 // indirect ) diff --git a/go.sum b/go.sum index 136b895..deb740f 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,7 @@ github.com/bwmarrin/discordgo v0.24.0 h1:Gw4MYxqHdvhO99A3nXnSLy97z5pmIKHZVJ1JY5Z github.com/bwmarrin/discordgo v0.24.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 h1:Izz0+t1Z5nI16/II7vuEo/nHjodOg0p7+OiDpjX5t1E= github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= @@ -23,10 +24,11 @@ github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.4.3-0.20220104015952-9111bb834a68 h1:m6V3+LXduRWqp1rg8FMHBagwmNsgXMVj5PHVLYnPeVQ= -github.com/gorilla/websocket v1.4.3-0.20220104015952-9111bb834a68/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= @@ -39,9 +41,10 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sleeyax/aternos-api v0.3.0 h1:SWPDt56j4qGkGnYnadLg0v13XS5Srg2iNWwPR87q9wM= -github.com/sleeyax/aternos-api v0.3.0/go.mod h1:+EI25UT/xG/cfiUsTNFTVL/UOmWqdczf9vOZEoogUkI= +github.com/sleeyax/aternos-api v0.3.4 h1:rJTx6DhnSNIwlPWaw9vJeAWyFO4GOa4iRqbsXvlsOHQ= +github.com/sleeyax/aternos-api v0.3.4/go.mod h1:H4c6r+BbsywQoJAd5pwsz+BNjh2RsX3wRj6gl0S+XUE= github.com/sleeyax/gotcha v0.0.2 h1:eiCFjEa4nwl/eZ7etBswDKPQ0bUp8YPbuicCHJtD68I= github.com/sleeyax/gotcha v0.0.2/go.mod h1:H2TKsKYJIXgmFGGUqs21FCr1HXVXjNWccX5/WexkLOM= github.com/sleeyax/gotcha/adapters/fhttp v0.0.0-20211225143732-1ae0bde30de1 h1:qlengZ6XaJeHZl4AgIOvcGMs4r75V6DFuP+7zjgQzo0= @@ -49,7 +52,9 @@ github.com/sleeyax/gotcha/adapters/fhttp v0.0.0-20211225143732-1ae0bde30de1/go.m github.com/sleeyax/utls v1.1.1 h1:tVapK30m6pEJd4zq5Cmq/SwmkhPsbxfik1bBagMpKgw= github.com/sleeyax/utls v1.1.1/go.mod h1:+D89TUtA8+NKVFj1IXWr0p3tSdX1+SqUB7rL0QnGqyg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/useflyent/fhttp v0.0.0-20210801005649-f160dd923789 h1:EnTm6BiUoGxi7fdeHMnSBnDyyKYT0AWq96V3cXouL4s= github.com/useflyent/fhttp v0.0.0-20210801005649-f160dd923789/go.mod h1:GTDLTqqiwTuUM1f9bCE/HoHOzBaCtT1Zjkd98vUEwrI= @@ -84,8 +89,9 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -96,6 +102,7 @@ golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 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-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -103,4 +110,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/kubernetes.yaml b/kubernetes.yaml index f6d83de..caaddff 100644 --- a/kubernetes.yaml +++ b/kubernetes.yaml @@ -28,6 +28,11 @@ spec: secretKeyRef: name: aternos-secrets key: DISCORD_TOKEN + - name: PROXY + valueFrom: + secretKeyRef: + name: aternos-secrets + key: PROXY --- apiVersion: apps/v1 kind: Deployment diff --git a/types.go b/types.go index 57dcfae..0acf450 100644 --- a/types.go +++ b/types.go @@ -4,6 +4,7 @@ import ( "github.com/bwmarrin/discordgo" "github.com/sleeyax/aternos-discord-bot/database" "github.com/sleeyax/aternos-discord-bot/worker" + "net/url" ) type Bot struct { @@ -13,6 +14,17 @@ type Bot struct { // Discord bot token. DiscordToken string + // HTTP(S) proxy to connect to. + // + // Format: protocol://username:password@host:port + // + // Examples: + // + // http://127.0.0.1:8888 + // + // http://username:password@1.2.3.4:1234 + Proxy *url.URL + // Current discord bot session. discord *discordgo.Session