/
listener.go
186 lines (149 loc) · 3.81 KB
/
listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package server
import (
"context"
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"log/slog"
"net"
"net/http"
"path/filepath"
"strings"
"text/template"
"github.com/xssnick/tonutils-go/adnl"
"github.com/xssnick/tonutils-go/adnl/dht"
rldphttp "github.com/xssnick/tonutils-go/adnl/rldp/http"
conf "github.com/ad/ton-site-ha/config"
"github.com/ad/ton-site-ha/site"
)
type Listener struct {
lgr *slog.Logger
config *conf.Config
Server *rldphttp.Server
}
func InitListener(lgr *slog.Logger, config *conf.Config) (*Listener, error) {
key, err := getKey(config.Key)
if err != nil {
return nil, err
}
listener := &Listener{
lgr: lgr,
config: config,
}
_, priv, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, err
}
gateway := adnl.NewGateway(priv)
err = gateway.StartClient()
if err != nil {
return nil, err
}
dhtClient, err := dht.NewClientFromConfigUrl(context.Background(), gateway, "https://ton.org/global.config.json")
if err != nil {
return nil, err
}
fs := http.FileServer(http.FS(site.Static))
// http.Handle("/static/", neuter(fs))
mx := http.NewServeMux()
mx.HandleFunc("/", listener.serveTemplate)
mx.Handle("/static/", neuter(fs))
s := rldphttp.NewServer(key, dhtClient, mx)
addr, err := rldphttp.SerializeADNLAddress(s.Address())
if err != nil {
return nil, err
}
addrHex, err := rldphttp.ParseADNLAddress(addr)
if err != nil {
return nil, err
}
publicIP := getPublicIP()
fmt.Println("Public IP:", publicIP)
fmt.Println("addr for TON DNS", hex.EncodeToString(addrHex))
fmt.Println("Listening on", addr+".adnl")
s.SetExternalIP(net.ParseIP(publicIP))
_, cancelCtx := context.WithCancel(context.Background())
go func(*rldphttp.Server) {
err := s.ListenAndServe(net.JoinHostPort(config.ListenHost, config.ListenPort))
if errors.Is(err, http.ErrServerClosed) {
lgr.Info("server closed")
} else if err != nil {
lgr.Error(fmt.Sprintf("error listening for server: %s", err))
}
cancelCtx()
}(s)
// http.HandleFunc("/", listener.serveTemplate)
// go func() {
// err := http.ListenAndServe(":3000", nil)
// if errors.Is(err, http.ErrServerClosed) {
// lgr.Info("server closed")
// } else if err != nil {
// lgr.Error(fmt.Sprintf("error listening for server: %s", err))
// }
// }()
listener.Server = s
return listener, nil
}
func (l *Listener) serveTemplate(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%+v\n", r)
if r.URL.Path == "" || r.URL.Path == "/" || strings.HasPrefix(r.URL.Path, "/?") {
r.URL.Path = "/index.html"
}
lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", filepath.Clean(r.URL.Path))
tmpl, err := template.New("base.html").ParseFS(site.Templates, lp, fp)
if err != nil {
log.Print(err.Error())
http.Error(w, http.StatusText(404), 404)
return
}
err = tmpl.ExecuteTemplate(w, "layout", nil)
if err != nil {
log.Print(err.Error())
http.Error(w, http.StatusText(404), 404)
}
}
func getKey(data string) (ed25519.PrivateKey, error) {
dec, err := hex.DecodeString(data)
if err != nil {
return nil, err
}
return ed25519.NewKeyFromSeed(dec), nil
}
func getPublicIP() string {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return err.Error()
}
defer req.Body.Close()
body, err := io.ReadAll(req.Body)
if err != nil {
return err.Error()
}
var ip struct {
Query string
}
_ = json.Unmarshal(body, &ip)
return ip.Query
}
func SubAndWrapFS(fSys fs.FS, dir string) http.FileSystem {
fSys, err := fs.Sub(fSys, dir)
if err != nil {
log.Fatal(err)
}
return http.FS(fSys)
}
func neuter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)
return
}
next.ServeHTTP(w, r)
})
}