From 6d4ae3d7194788ad9c66095971dba288a2ba7d38 Mon Sep 17 00:00:00 2001 From: Brian Olson Date: Fri, 6 Sep 2024 16:19:55 -0400 Subject: [PATCH 1/5] quick integration of ipcc service --- bskyweb/cmd/bskyweb/main.go | 6 +++ bskyweb/cmd/bskyweb/server.go | 69 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/bskyweb/cmd/bskyweb/main.go b/bskyweb/cmd/bskyweb/main.go index d9235afd..306b8f2e 100644 --- a/bskyweb/cmd/bskyweb/main.go +++ b/bskyweb/cmd/bskyweb/main.go @@ -60,6 +60,12 @@ func run(args []string) { Value: "", EnvVars: []string{"LINK_HOST"}, }, + &cli.StringFlag{ + Name: "ipcc-host", + Usage: "scheme, hostname, and port of ipcc service", + Value: "https://localhost:8730", + EnvVars: []string{"IPCC_HOST", "IPCC_HOST"}, + }, &cli.BoolFlag{ Name: "debug", Usage: "Enable debug mode", diff --git a/bskyweb/cmd/bskyweb/server.go b/bskyweb/cmd/bskyweb/server.go index 203ed62f..afd9247a 100644 --- a/bskyweb/cmd/bskyweb/server.go +++ b/bskyweb/cmd/bskyweb/server.go @@ -1,12 +1,17 @@ package main import ( + "bytes" "context" "crypto/subtle" + "crypto/tls" + "encoding/base64" + "encoding/json" "errors" "fmt" "io/fs" "net/http" + "net/netip" "net/url" "os" "os/signal" @@ -41,6 +46,7 @@ type Config struct { appviewHost string ogcardHost string linkHost string + ipccHost string } func serve(cctx *cli.Context) error { @@ -49,6 +55,7 @@ func serve(cctx *cli.Context) error { appviewHost := cctx.String("appview-host") ogcardHost := cctx.String("ogcard-host") linkHost := cctx.String("link-host") + ipccHost := cctx.String("ipcc-host") basicAuthPassword := cctx.String("basic-auth-password") // Echo @@ -91,6 +98,7 @@ func serve(cctx *cli.Context) error { appviewHost: appviewHost, ogcardHost: ogcardHost, linkHost: linkHost, + ipccHost: ipccHost, }, } @@ -261,6 +269,9 @@ func serve(cctx *cli.Context) error { e.GET("/starter-pack/:handleOrDID/:rkey", server.WebStarterPack) e.GET("/start/:handleOrDID/:rkey", server.WebStarterPack) + // ipcc + e.GET("/ipcc", server.WebIpCC) + if linkHost != "" { linkUrl, err := url.Parse(linkHost) if err != nil { @@ -520,3 +531,61 @@ func (srv *Server) WebProfile(c echo.Context) error { data["requestHost"] = req.Host return c.Render(http.StatusOK, "profile.html", data) } + +type IPCCRequest struct { + IP string `json:"ip"` +} +type IPCCResponse struct { + CC string `json:"countryCode"` +} + +func (srv *Server) WebIpCC(c echo.Context) error { + realIP := c.RealIP() + addr, err := netip.ParseAddr(realIP) + if err != nil { + log.Warnf("could not parse IP %q %s", realIP, err) + return c.JSON(400, IPCCResponse{}) + } + var request []byte + if addr.Is4() { + ip4 := addr.As4() + var dest [8]byte + base64.StdEncoding.Encode(dest[:], ip4[:]) + request, _ = json.Marshal(IPCCRequest{IP: string(dest[:])}) + } else if addr.Is6() { + ip6 := addr.As16() + var dest [24]byte + base64.StdEncoding.Encode(dest[:], ip6[:]) + request, _ = json.Marshal(IPCCRequest{IP: string(dest[:])}) + } + + ipccUrlBuilder, err := url.Parse(srv.cfg.ipccHost) + if err != nil { + log.Errorf("ipcc misconfigured bad url %s", err) + return c.JSON(500, IPCCResponse{}) + } + ipccUrlBuilder.Path = "ipccdata.IpCcService/Lookup" + ipccUrl := ipccUrlBuilder.String() + cl := http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, + } + postBodyReader := bytes.NewReader(request) + response, err := cl.Post(ipccUrl, "application/json", postBodyReader) + if err != nil { + log.Warnf("ipcc backend error %s", err) + return c.JSON(500, IPCCResponse{}) + } + defer response.Body.Close() + dec := json.NewDecoder(response.Body) + var outResponse IPCCResponse + err = dec.Decode(&outResponse) + if err != nil { + log.Warnf("ipcc bad response %s", err) + return c.JSON(500, IPCCResponse{}) + } + return c.JSON(200, outResponse) +} From eec93c4e74fb1c87cb490c5a42c377e1bab41454 Mon Sep 17 00:00:00 2001 From: Brian Olson Date: Fri, 6 Sep 2024 16:34:57 -0400 Subject: [PATCH 2/5] cleanup --- bskyweb/cmd/bskyweb/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bskyweb/cmd/bskyweb/main.go b/bskyweb/cmd/bskyweb/main.go index 306b8f2e..3f46c4b0 100644 --- a/bskyweb/cmd/bskyweb/main.go +++ b/bskyweb/cmd/bskyweb/main.go @@ -64,7 +64,7 @@ func run(args []string) { Name: "ipcc-host", Usage: "scheme, hostname, and port of ipcc service", Value: "https://localhost:8730", - EnvVars: []string{"IPCC_HOST", "IPCC_HOST"}, + EnvVars: []string{"IPCC_HOST"}, }, &cli.BoolFlag{ Name: "debug", From d41a00b373ada4c3566cc5af57f232b34604cc02 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 6 Sep 2024 14:58:19 -0700 Subject: [PATCH 3/5] Trigger a build maybe --- .github/workflows/build-and-push-bskyweb-aws.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-and-push-bskyweb-aws.yaml b/.github/workflows/build-and-push-bskyweb-aws.yaml index 6eb9485b..01f08137 100644 --- a/.github/workflows/build-and-push-bskyweb-aws.yaml +++ b/.github/workflows/build-and-push-bskyweb-aws.yaml @@ -4,6 +4,8 @@ on: push: branches: - main + pull_request: + branches: [brianolson:ipcc-handler] env: REGISTRY: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_REGISTRY }} From 6620ee421b125b1ae63a85e29e3e947094fb24c8 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 6 Sep 2024 14:59:02 -0700 Subject: [PATCH 4/5] fix action --- .github/workflows/build-and-push-bskyweb-aws.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-push-bskyweb-aws.yaml b/.github/workflows/build-and-push-bskyweb-aws.yaml index 01f08137..ddaefbf6 100644 --- a/.github/workflows/build-and-push-bskyweb-aws.yaml +++ b/.github/workflows/build-and-push-bskyweb-aws.yaml @@ -5,7 +5,7 @@ on: branches: - main pull_request: - branches: [brianolson:ipcc-handler] + branches: [main] env: REGISTRY: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_REGISTRY }} From dc6b04b66fe0d9de0e4fd311172cae3b22fcfeb6 Mon Sep 17 00:00:00 2001 From: Jaz Date: Fri, 6 Sep 2024 15:00:13 -0700 Subject: [PATCH 5/5] nvm --- .github/workflows/build-and-push-bskyweb-aws.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/build-and-push-bskyweb-aws.yaml b/.github/workflows/build-and-push-bskyweb-aws.yaml index ddaefbf6..6eb9485b 100644 --- a/.github/workflows/build-and-push-bskyweb-aws.yaml +++ b/.github/workflows/build-and-push-bskyweb-aws.yaml @@ -4,8 +4,6 @@ on: push: branches: - main - pull_request: - branches: [main] env: REGISTRY: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_REGISTRY }}