Files
about.i2p/main.go

123 lines
3.0 KiB
Go
Raw Normal View History

2022-09-28 18:52:57 -04:00
package main
import (
2022-09-28 23:00:16 -04:00
"crypto/rand"
"encoding/base32"
2022-09-28 18:52:57 -04:00
"flag"
"fmt"
"io/ioutil"
2022-09-28 18:52:57 -04:00
"log"
"net/http"
"net/url"
"os"
"strings"
"cerca/util"
"github.com/eyedeekay/about.i2p/about"
2022-09-29 01:32:37 -04:00
"github.com/eyedeekay/goSam"
2022-09-28 18:52:57 -04:00
"github.com/eyedeekay/onramp"
)
func readAllowlist(location string) []string {
ed := util.Describe("read allowlist")
data, err := os.ReadFile(location)
ed.Check(err, "read file")
list := strings.Split(strings.TrimSpace(string(data)), "\n")
var processed []string
for _, fullpath := range list {
u, err := url.Parse(fullpath)
if err != nil {
continue
}
processed = append(processed, u.Host)
}
return processed
}
func complain(msg string) {
fmt.Printf("cerca: %s\n", msg)
os.Exit(0)
}
func main() {
var allowlistLocation string
var sessionKey string
2022-09-28 23:00:16 -04:00
var genAuthKey bool
2022-09-28 18:52:57 -04:00
var dir string
flag.StringVar(&allowlistLocation, "allowlist", "", "domains which can be used to read verification codes from during registration")
flag.StringVar(&sessionKey, "authkey", "", "session cookies authentication key")
2022-09-28 23:00:16 -04:00
flag.BoolVar(&genAuthKey, "genauthkey", false, "generate a valid session cookies authentication key")
2022-09-28 18:52:57 -04:00
flag.StringVar(&dir, "dir", "", "directory to run in")
flag.Parse()
2022-09-28 23:00:16 -04:00
if genAuthKey {
c := 64
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
fmt.Println("error:", err)
return
}
// The slice should now contain random bytes instead of only zeroes.
//fmt.Println(bytes.Equal(b, make([]byte, c)))
dst := make([]byte, base32.StdEncoding.EncodedLen(len(b)))
base32.StdEncoding.Encode(dst, b)
fmt.Println(string(dst))
//fmt.Println(b)
os.Exit(0)
}
2022-09-28 18:52:57 -04:00
if len(sessionKey) == 0 {
complain("please pass a random session auth key with --authkey")
} else if len(allowlistLocation) == 0 {
//complain("please pass a file containing the verification code domain allowlist")
allowlistLocation = "allow.txt"
if err := ioutil.WriteFile(allowlistLocation, []byte(""), 0644); err != nil {
panic(err)
}
}
if _, err := os.Stat(allowlistLocation); os.IsNotExist(err) {
if err := ioutil.WriteFile(allowlistLocation, []byte(""), 0644); err != nil {
panic(err)
}
2022-09-28 18:52:57 -04:00
}
garlic, err := onramp.NewGarlic("about.i2p", "127.0.0.1:7656", []string{})
if err != nil {
panic(err)
}
2022-09-29 00:25:09 -04:00
defer garlic.Close()
2022-09-29 01:32:37 -04:00
sam, err := goSam.NewDefaultClient()
if err != nil {
panic(err)
}
log.Println("Client Created")
// create a transport that uses SAM to dial TCP Connections
2022-09-29 01:32:45 -04:00
http.DefaultClient = &http.Client{
2022-09-29 01:25:30 -04:00
Transport: &http.Transport{
2022-09-29 01:32:37 -04:00
Dial: sam.Dial,
2022-09-29 01:25:30 -04:00
},
2022-09-28 18:52:57 -04:00
}
2022-09-29 01:32:37 -04:00
2022-09-28 18:52:57 -04:00
allowList := readAllowlist(allowlistLocation)
allowList = append(allowList, "*.i2p")
allowList = append(allowList, "*.b32.i2p")
if ln, err := garlic.ListenTLS(); err != nil {
panic(err)
} else {
2022-09-29 00:25:09 -04:00
defer ln.Close()
2022-09-28 18:52:57 -04:00
allowList = append(allowList, ln.Addr().String())
if cercaServer, err := about.NewServer(allowList, sessionKey, dir); err != nil {
panic(err)
} else {
if err := http.Serve(ln, cercaServer); err != nil {
panic(err)
} else {
log.Println("Exited gracefully")
}
}
}
}