Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #5195 from brianolson/ipcc-handler

quick integration of ipcc service

authored by

Jaz and committed by
GitHub
30d2ab8d 7e4f8cab

+75
+6
bskyweb/cmd/bskyweb/main.go
··· 60 60 Value: "", 61 61 EnvVars: []string{"LINK_HOST"}, 62 62 }, 63 + &cli.StringFlag{ 64 + Name: "ipcc-host", 65 + Usage: "scheme, hostname, and port of ipcc service", 66 + Value: "https://localhost:8730", 67 + EnvVars: []string{"IPCC_HOST"}, 68 + }, 63 69 &cli.BoolFlag{ 64 70 Name: "debug", 65 71 Usage: "Enable debug mode",
+69
bskyweb/cmd/bskyweb/server.go
··· 1 1 package main 2 2 3 3 import ( 4 + "bytes" 4 5 "context" 5 6 "crypto/subtle" 7 + "crypto/tls" 8 + "encoding/base64" 9 + "encoding/json" 6 10 "errors" 7 11 "fmt" 8 12 "io/fs" 9 13 "net/http" 14 + "net/netip" 10 15 "net/url" 11 16 "os" 12 17 "os/signal" ··· 41 46 appviewHost string 42 47 ogcardHost string 43 48 linkHost string 49 + ipccHost string 44 50 } 45 51 46 52 func serve(cctx *cli.Context) error { ··· 49 55 appviewHost := cctx.String("appview-host") 50 56 ogcardHost := cctx.String("ogcard-host") 51 57 linkHost := cctx.String("link-host") 58 + ipccHost := cctx.String("ipcc-host") 52 59 basicAuthPassword := cctx.String("basic-auth-password") 53 60 54 61 // Echo ··· 91 98 appviewHost: appviewHost, 92 99 ogcardHost: ogcardHost, 93 100 linkHost: linkHost, 101 + ipccHost: ipccHost, 94 102 }, 95 103 } 96 104 ··· 261 269 e.GET("/starter-pack/:handleOrDID/:rkey", server.WebStarterPack) 262 270 e.GET("/start/:handleOrDID/:rkey", server.WebStarterPack) 263 271 272 + // ipcc 273 + e.GET("/ipcc", server.WebIpCC) 274 + 264 275 if linkHost != "" { 265 276 linkUrl, err := url.Parse(linkHost) 266 277 if err != nil { ··· 520 531 data["requestHost"] = req.Host 521 532 return c.Render(http.StatusOK, "profile.html", data) 522 533 } 534 + 535 + type IPCCRequest struct { 536 + IP string `json:"ip"` 537 + } 538 + type IPCCResponse struct { 539 + CC string `json:"countryCode"` 540 + } 541 + 542 + func (srv *Server) WebIpCC(c echo.Context) error { 543 + realIP := c.RealIP() 544 + addr, err := netip.ParseAddr(realIP) 545 + if err != nil { 546 + log.Warnf("could not parse IP %q %s", realIP, err) 547 + return c.JSON(400, IPCCResponse{}) 548 + } 549 + var request []byte 550 + if addr.Is4() { 551 + ip4 := addr.As4() 552 + var dest [8]byte 553 + base64.StdEncoding.Encode(dest[:], ip4[:]) 554 + request, _ = json.Marshal(IPCCRequest{IP: string(dest[:])}) 555 + } else if addr.Is6() { 556 + ip6 := addr.As16() 557 + var dest [24]byte 558 + base64.StdEncoding.Encode(dest[:], ip6[:]) 559 + request, _ = json.Marshal(IPCCRequest{IP: string(dest[:])}) 560 + } 561 + 562 + ipccUrlBuilder, err := url.Parse(srv.cfg.ipccHost) 563 + if err != nil { 564 + log.Errorf("ipcc misconfigured bad url %s", err) 565 + return c.JSON(500, IPCCResponse{}) 566 + } 567 + ipccUrlBuilder.Path = "ipccdata.IpCcService/Lookup" 568 + ipccUrl := ipccUrlBuilder.String() 569 + cl := http.Client{ 570 + Transport: &http.Transport{ 571 + TLSClientConfig: &tls.Config{ 572 + InsecureSkipVerify: true, 573 + }, 574 + }, 575 + } 576 + postBodyReader := bytes.NewReader(request) 577 + response, err := cl.Post(ipccUrl, "application/json", postBodyReader) 578 + if err != nil { 579 + log.Warnf("ipcc backend error %s", err) 580 + return c.JSON(500, IPCCResponse{}) 581 + } 582 + defer response.Body.Close() 583 + dec := json.NewDecoder(response.Body) 584 + var outResponse IPCCResponse 585 + err = dec.Decode(&outResponse) 586 + if err != nil { 587 + log.Warnf("ipcc bad response %s", err) 588 + return c.JSON(500, IPCCResponse{}) 589 + } 590 + return c.JSON(200, outResponse) 591 + }