Skip to content

Commit

Permalink
Explicit external ip support
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Jan 11, 2023
1 parent 0b5eaef commit 59cc938
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
29 changes: 23 additions & 6 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"github.com/mdp/qrterminal/v3"
"github.com/sigurn/crc16"
"github.com/xssnick/tonutils-go/adnl"
"github.com/xssnick/tonutils-go/adnl/dht"
rldphttp "github.com/xssnick/tonutils-go/adnl/rldp/http"
"github.com/xssnick/tonutils-go/liteclient"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/xssnick/tonutils-go/ton/dns"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
Expand All @@ -27,13 +29,14 @@ import (

type Config struct {
ProxyPass string `json:"proxy_pass"`
ForwardTo string `json:"forward_to"`
PrivateKey []byte `json:"private_key"`
IP string `json:"address"`
ExternalIP string `json:"external_ip"`
ListenIP string `json:"listen_ip"`
Port uint16 `json:"port"`
}

var FlagDomain = flag.String("domain", "", "domain to configure")
var FlagDebug = flag.Bool("debug", false, "more logs")

type Handler struct {
h http.Handler
Expand All @@ -49,7 +52,7 @@ func (h Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {

log.Println("request:", request.Method, request.Host, request.RequestURI)

writer.Header().Set("Ton-Reverse-Proxy", "TonUtils Reverse Proxy v0.0.2")
writer.Header().Set("Ton-Reverse-Proxy", "TonUtils Reverse Proxy v0.0.4")
h.h.ServeHTTP(writer, request)
}

Expand Down Expand Up @@ -86,8 +89,14 @@ func main() {
panic(err)
}

if *FlagDebug == false {
adnl.Logger = func(v ...any) {}
// rldphttp.Logger = func(v ...any) {}
}

proxy := httputil.NewSingleHostReverseProxy(u)
s := rldphttp.NewServer(ed25519.NewKeyFromSeed(cfg.PrivateKey), dhtClient, Handler{proxy})
s.SetExternalIP(net.ParseIP(cfg.ExternalIP))

addr, err := rldphttp.SerializeADNLAddress(s.Address())
if err != nil {
Expand All @@ -100,7 +109,7 @@ func main() {
}

log.Println("Starting server on", addr+".adnl")
if err = s.ListenAndServe(fmt.Sprintf("%s:%d", cfg.IP, cfg.Port)); err != nil {
if err = s.ListenAndServe(fmt.Sprintf("%s:%d", cfg.ListenIP, cfg.Port)); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -141,13 +150,14 @@ func loadConfig() (*Config, error) {
}
cfg.PrivateKey = srvKey.Seed()

cfg.IP, err = getPublicIP()
cfg.ExternalIP, err = getPublicIP()
if err != nil {
return nil, err
}
cfg.ListenIP = "0.0.0.0"

// generate consistent port
cfg.Port = 9000 + (crc16.Checksum([]byte(cfg.IP), crc16.MakeTable(crc16.CRC16_XMODEM)) % 5000)
cfg.Port = 9000 + (crc16.Checksum([]byte(cfg.ExternalIP), crc16.MakeTable(crc16.CRC16_XMODEM)) % 5000)

cfg.ProxyPass = "http://127.0.0.1:80/"

Expand Down Expand Up @@ -194,8 +204,15 @@ func setupDomain(client *liteclient.ConnectionPool, domain string, adnlAddr []by
data := domainInfo.BuildSetSiteRecordPayload(adnlAddr).ToBOCWithFlags(false)
args := "?bin=" + base64.URLEncoding.EncodeToString(data) + "&amount=" + tlb.MustFromTON("0.02").NanoTON().String()

nftData, err := domainInfo.GetNFTData(context.Background())
if err != nil {
log.Println("Failed to get domain data", domain, ":", err)
return
}

qrterminal.GenerateHalfBlock("ton://transfer/"+domainInfo.GetNFTAddress().String()+args, qrterminal.L, os.Stdout)
fmt.Println("Execute this transaction from the domain owner's wallet to setup site records.")
fmt.Println("Execute transaction from wallet:", nftData.OwnerAddress.String())
fmt.Println("When you've done, configuration will automatically proceed in ~10 seconds.")
for {
time.Sleep(5 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/mdp/qrterminal/v3 v3.0.0
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3
github.com/xssnick/tonutils-go v1.4.1-0.20230109130346-b0cd607f9ab5
github.com/xssnick/tonutils-go v1.4.1-0.20230111070238-84e56fb29fc5
)

require (
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ github.com/xssnick/tonutils-go v1.4.1-0.20230109125855-ce4e62c44be4 h1:ihkJUII8d
github.com/xssnick/tonutils-go v1.4.1-0.20230109125855-ce4e62c44be4/go.mod h1:wH8ldhLueyfXW15r3MyaIq9YzA+8bzvL6UMU2BLp08g=
github.com/xssnick/tonutils-go v1.4.1-0.20230109130346-b0cd607f9ab5 h1:BkYqyX87xkZ1MmlP5SGV42lSnewJPrB2WCArPDzaVCI=
github.com/xssnick/tonutils-go v1.4.1-0.20230109130346-b0cd607f9ab5/go.mod h1:wH8ldhLueyfXW15r3MyaIq9YzA+8bzvL6UMU2BLp08g=
github.com/xssnick/tonutils-go v1.4.1-0.20230111063410-3fe2a189cd60 h1:Tc9Lm9G3VGjIb29/tjKk2tgwlSFBIvnwpgGaMF6B+4U=
github.com/xssnick/tonutils-go v1.4.1-0.20230111063410-3fe2a189cd60/go.mod h1:wH8ldhLueyfXW15r3MyaIq9YzA+8bzvL6UMU2BLp08g=
github.com/xssnick/tonutils-go v1.4.1-0.20230111065721-26547c5ebc1e h1:V09j1fNm1pWsdULpeTic2r16nqH00A41kBlzjurc/UA=
github.com/xssnick/tonutils-go v1.4.1-0.20230111065721-26547c5ebc1e/go.mod h1:wH8ldhLueyfXW15r3MyaIq9YzA+8bzvL6UMU2BLp08g=
github.com/xssnick/tonutils-go v1.4.1-0.20230111070238-84e56fb29fc5 h1:21bO7IXo0gjG5HwkMg6VqbOMUzwP7C4eMeMFACjGuVw=
github.com/xssnick/tonutils-go v1.4.1-0.20230111070238-84e56fb29fc5/go.mod h1:wH8ldhLueyfXW15r3MyaIq9YzA+8bzvL6UMU2BLp08g=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down

0 comments on commit 59cc938

Please sign in to comment.