Merge pull request #5195 from brianolson/ipcc-handler

quick integration of ipcc service
zio/stable
Jaz 2024-09-06 15:21:27 -07:00 committed by GitHub
commit 30d2ab8dd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 0 deletions

View File

@ -60,6 +60,12 @@ func run(args []string) {
Value: "", Value: "",
EnvVars: []string{"LINK_HOST"}, 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"},
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "Enable debug mode", Usage: "Enable debug mode",

View File

@ -1,12 +1,17 @@
package main package main
import ( import (
"bytes"
"context" "context"
"crypto/subtle" "crypto/subtle"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"net/http" "net/http"
"net/netip"
"net/url" "net/url"
"os" "os"
"os/signal" "os/signal"
@ -41,6 +46,7 @@ type Config struct {
appviewHost string appviewHost string
ogcardHost string ogcardHost string
linkHost string linkHost string
ipccHost string
} }
func serve(cctx *cli.Context) error { func serve(cctx *cli.Context) error {
@ -49,6 +55,7 @@ func serve(cctx *cli.Context) error {
appviewHost := cctx.String("appview-host") appviewHost := cctx.String("appview-host")
ogcardHost := cctx.String("ogcard-host") ogcardHost := cctx.String("ogcard-host")
linkHost := cctx.String("link-host") linkHost := cctx.String("link-host")
ipccHost := cctx.String("ipcc-host")
basicAuthPassword := cctx.String("basic-auth-password") basicAuthPassword := cctx.String("basic-auth-password")
// Echo // Echo
@ -91,6 +98,7 @@ func serve(cctx *cli.Context) error {
appviewHost: appviewHost, appviewHost: appviewHost,
ogcardHost: ogcardHost, ogcardHost: ogcardHost,
linkHost: linkHost, linkHost: linkHost,
ipccHost: ipccHost,
}, },
} }
@ -261,6 +269,9 @@ func serve(cctx *cli.Context) error {
e.GET("/starter-pack/:handleOrDID/:rkey", server.WebStarterPack) e.GET("/starter-pack/:handleOrDID/:rkey", server.WebStarterPack)
e.GET("/start/:handleOrDID/:rkey", server.WebStarterPack) e.GET("/start/:handleOrDID/:rkey", server.WebStarterPack)
// ipcc
e.GET("/ipcc", server.WebIpCC)
if linkHost != "" { if linkHost != "" {
linkUrl, err := url.Parse(linkHost) linkUrl, err := url.Parse(linkHost)
if err != nil { if err != nil {
@ -520,3 +531,61 @@ func (srv *Server) WebProfile(c echo.Context) error {
data["requestHost"] = req.Host data["requestHost"] = req.Host
return c.Render(http.StatusOK, "profile.html", data) 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)
}