diff options
author | terminaldweller <devi@terminaldweller.com> | 2024-01-23 17:54:13 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2024-01-23 17:54:13 +0000 |
commit | 1f220bffe0b2f8818151b487a83e1a0b76a07fb5 (patch) | |
tree | 75c533fb380b23c64e00b520d64925b48ea5fd15 | |
parent | WIP (diff) | |
download | sms-webhook-1f220bffe0b2f8818151b487a83e1a0b76a07fb5.tar.gz sms-webhook-1f220bffe0b2f8818151b487a83e1a0b76a07fb5.zip |
first working version
Diffstat (limited to '')
-rw-r--r-- | Dockerfile | 2 | ||||
-rw-r--r-- | docker-compose.yaml | 3 | ||||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 6 | ||||
-rw-r--r-- | main.go | 146 |
5 files changed, 96 insertions, 62 deletions
@@ -4,8 +4,8 @@ RUN apk update && apk upgrade && \ RUN git clone https://github.com/terminaldweller/sms-webhook WORKDIR /sms-webhook COPY go.sum go.mod /sms-webhook/ -COPY *.go /sms-webhook/ RUN go mod download +COPY *.go /sms-webhook/ ENV CGO_ENABLED=0 RUN go build diff --git a/docker-compose.yaml b/docker-compose.yaml index a9b5dfb..2da6870 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -24,12 +24,15 @@ services: # - /etc/letsencrypt/live/sms.terminaldweller.com/privkey.pem:/etc/letsencrypt/live/sms.terminaldweller.com/privkey.pem cap_drop: - ALL + dns: + - 9.9.9.9 environment: - SERVER_DEPLOYMENT_TYPE=deployment entrypoint: ["/sms-webhook/sms-webhook"] command: ["serve", "--http=0.0.0.0:8090"] volumes: - pb-vault:/sms-webhook/pb_data + - ./config.toml:/opt/smswebhook/config.toml redis: image: redis:7.2.3-alpine3.18 deploy: @@ -6,6 +6,7 @@ require ( github.com/go-redis/redis/v8 v8.11.5 github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61 github.com/lrstanley/girc v0.0.0-20230911164840-f47717952bf9 + github.com/pelletier/go-toml/v2 v2.1.1 github.com/pocketbase/pocketbase v0.18.9 ) @@ -184,6 +184,8 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= +github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= 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= @@ -211,8 +213,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= @@ -2,14 +2,16 @@ package main import ( "crypto/tls" - "flag" "fmt" "log" "net/http" + "os" + "time" "github.com/go-redis/redis/v8" "github.com/labstack/echo/v5" "github.com/lrstanley/girc" + "github.com/pelletier/go-toml/v2" "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/core" ) @@ -22,7 +24,13 @@ const ( redisWriteTimetout = 10 ) -type SMSInfo struct { +type handlerWrapper struct { + irc *girc.Client + // irc chan *girc.Client + config TomlConfig +} + +type SMS struct { From string `json:"from"` Text string `json:"text"` SentStamp int64 `json:"sentStamp"` @@ -30,92 +38,108 @@ type SMSInfo struct { Sim string `json:"sim"` } -type IRCInfo struct { - ircServer string - ircPort int - ircNick string - ircSaslUser string - ircSaslPass string - ircChannel string +type TomlConfig struct { + IrcServer string + IrcPort int + IrcNick string + IrcSaslUser string + IrcSaslPass string + IrcChannel string + RedisAddress string + RedisPassword string + RedisDB int } -func postHandler(context echo.Context) error { - smsInfo := new(SMSInfo) - if err := context.Bind(smsInfo); err != nil { +// curl -X 'POST' 'http://127.0.0.1:8090/sms' -H 'content-type: application/json; charset=utf-8' -d $'{"from":"1234567890","text":"Test"}' +func (hw handlerWrapper) postHandler(context echo.Context) error { + sms := new(SMS) + if err := context.Bind(sms); err != nil { return context.String(http.StatusBadRequest, "bad request") } - smsInfoReal := SMSInfo{ - From: smsInfo.From, - Text: smsInfo.Text, - SentStamp: smsInfo.SentStamp, - ReceivedStamp: smsInfo.ReceivedStamp, - Sim: smsInfo.Sim, + smsInfoReal := SMS{ + From: sms.From, + Text: sms.Text, + SentStamp: sms.SentStamp, + ReceivedStamp: sms.ReceivedStamp, + Sim: sms.Sim, + } + + for { + fmt.Println("one") + // irc = <-hw.irc + if hw.irc != nil { + if hw.irc.IsConnected() { + break + } + } + fmt.Println("two") } + hw.irc.Cmd.Message(hw.config.IrcChannel, fmt.Sprintf("From: %s, Text: %s", sms.From, sms.Text)) + fmt.Println(smsInfoReal) - return context.JSON(http.StatusOK, smsInfo) + return context.JSON(http.StatusOK, sms) } -func runIRC(ircInfo IRCInfo) *girc.Client { +func runIRC(appConfig TomlConfig, ircChan chan *girc.Client) { irc := girc.New(girc.Config{ - Server: ircInfo.ircServer, - Port: ircInfo.ircPort, - Nick: ircInfo.ircNick, - User: "soulshack", - Name: "soulshack", + Server: appConfig.IrcServer, + Port: appConfig.IrcPort, + Nick: appConfig.IrcNick, + User: appConfig.IrcNick, + Name: appConfig.IrcNick, SSL: true, - TLSConfig: &tls.Config{InsecureSkipVerify: false}, + TLSConfig: &tls.Config{InsecureSkipVerify: true}, }) - saslUser := ircInfo.ircSaslUser - saslPass := ircInfo.ircSaslPass + saslUser := appConfig.IrcSaslUser + saslPass := appConfig.IrcSaslPass if saslUser != "" && saslPass != "" { irc.Config.SASL = &girc.SASLPlain{ - User: ircInfo.ircSaslUser, - Pass: ircInfo.ircSaslPass, + User: appConfig.IrcSaslUser, + Pass: appConfig.IrcSaslPass, } } - irc.Handlers.AddBg(girc.PRIVMSG, func(c *girc.Client, e girc.Event) { + irc.Handlers.AddBg(girc.CONNECTED, func(c *girc.Client, e girc.Event) { + c.Cmd.Join(appConfig.IrcChannel) }) - if err := irc.Connect(); err != nil { - log.Fatal(err) - return nil + // irc.Handlers.AddBg(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {}) + ircChan <- irc + + for { + if err := irc.Connect(); err != nil { + log.Println(err) + log.Println("reconnecting in 30 seconds") + time.Sleep(30 * time.Second) + } else { + return + } } - return irc } func main() { - ircServer := flag.String("ircserver", "irc.terminaldweller.com", "the address of the irc server to connect to") - ircPort := flag.Int("ircport", 6697, "the port of the irc server to connect to") - ircNick := flag.String("ircnick", "soulhack", "the nick to use on the irc server") - ircSaslUser := flag.String("ircsasluser", "soulhack", "the sasl user to use on the irc server") - ircSaslPass := flag.String("ircsaslpass", "", "the sasl password to use on the irc server") - ircChannel := flag.String("ircchannel", "#soulhack", "the channel to join on the irc server") - - ircInfo := IRCInfo{ - ircServer: *ircServer, - ircPort: *ircPort, - ircNick: *ircNick, - ircSaslUser: *ircSaslUser, - ircSaslPass: *ircSaslPass, - ircChannel: *ircChannel, + var appConfig TomlConfig + + data, err := os.ReadFile("/opt/smswebhook/config.toml") + if err != nil { + log.Fatal(err) } - ircClient := runIRC(ircInfo) + err = toml.Unmarshal(data, &appConfig) + if err != nil { + log.Fatal(err) + } - redisAddress := flag.String("redisaddress", "redis:6379", "determines the address of the redis instance") - redisPassword := flag.String("redispassword", "", "determines the password of the redis db") - redisDB := flag.Int64("redisdb", 0, "determines the db number") - flag.Parse() + log.Println(appConfig) rdb = redis.NewClient(&redis.Options{ - Addr: *redisAddress, - Password: *redisPassword, - DB: int(*redisDB), + Addr: appConfig.RedisAddress, + Password: appConfig.RedisPassword, + DB: appConfig.RedisDB, DialTimeout: redisDialTimeout, ReadTimeout: redisReadTimeout, WriteTimeout: redisWriteTimetout, @@ -124,10 +148,14 @@ func main() { app := pocketbase.New() + ircChan := make(chan *girc.Client, 1) + hw := handlerWrapper{irc: nil, config: appConfig} + app.OnBeforeServe().Add(func(e *core.ServeEvent) error { - e.Router.POST("/sms", postHandler) + go runIRC(appConfig, ircChan) + hw.irc = <-ircChan - ircClient.Handlers.AddBg(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {}) + e.Router.POST("/sms", hw.postHandler) return nil }) |