Files
trpic/main.go
2026-04-04 23:17:01 +08:00

106 lines
2.8 KiB
Go

package main
import (
"context"
"crypto/subtle"
"fmt"
"log"
"net/http"
"github.com/opencoff/go-srp"
"trle5.xyz/gopkg/trpic/src/configs"
"trle5.xyz/gopkg/trpic/src/database"
"trle5.xyz/gopkg/trpic/src/fetch_artworks/pixiv"
"trle5.xyz/gopkg/trpic/src/utils/auth"
"trle5.xyz/gopkg/trpic/src/web"
"trle5.xyz/gopkg/trpic/src/web/router"
)
func main2() {
password := []byte("password string that's too long")
username := []byte("foouser")
tSRP, err := srp.New(1024)
if err != nil { panic(err) }
tVerifier, err := tSRP.Verifier(username, password)
if err != nil { panic(err) }
ih, vh := tVerifier.Encode()
// Store ih, vh in durable storage
fmt.Printf("Verifier Store:\n %s => %s\n", ih, vh)
client, err := tSRP.NewClient(username, password)
if err != nil { panic(err) }
// client credentials (public key and identity) to send to server
creds := client.Credentials()
fmt.Printf("Client Begin; <I, A> --> server:\n %s\n", creds)
// Begin the server by parsing the client public key and identity.
ih, A, err := srp.ServerBegin(creds)
if err != nil { panic(err) }
// Now, pretend to lookup the user db using "I" as the key and
// fetch salt, verifier etc.
tSRP, tVerifier, err = srp.MakeSRPVerifier(vh)
if err != nil { panic(err) }
fmt.Printf("Server Begin; <v, A>:\n %s\n %x\n", vh, A.Bytes())
srv, err := tSRP.NewServer(tVerifier, A)
if err != nil { panic(err) }
// Generate the credentials to send to client
creds = srv.Credentials()
// Send the server public key and salt to server
fmt.Printf("Server Begin; <s, B> --> client:\n %s\n", creds)
// client processes the server creds and generates
// a mutual authenticator; the authenticator is sent
// to the server as proof that the client derived its keys.
cauth, err := client.Generate(creds)
if err != nil { panic(err) }
fmt.Printf("Client Authenticator: M --> Server\n %s\n", cauth)
// Receive the proof of authentication from client
proof, ok := srv.ClientOk(cauth)
if !ok { panic("client auth failed") }
// Send proof to the client
fmt.Printf("Server Authenticator: M' --> Server\n %s\n", proof)
// Verify the server's proof
if !client.ServerOk(proof) { panic("server auth failed") }
// Now, we have successfully authenticated the client to the
// server and vice versa.
kc := client.RawKey()
ks := srv.RawKey()
if 1 != subtle.ConstantTimeCompare(kc, ks) {
panic("Keys are different!")
}
fmt.Printf("Client Key: %x\nServer Key: %x\n", kc, ks)
}
func main() {
configs.LoadConfig()
database.Init()
auth.CheckRegister(context.Background())
web.Init()
router.Init()
pixiv.InitApp(configs.Config.PixivRefreshToken)
// pixiv.InitWeb(trpic.Config.PixivPHPSession)
// pixiv.InitCrawler(trpic.Config.PixivRefreshToken)
log.Println("服务器启动,访问 http://localhost:8080 进行预览")
log.Fatal(http.ListenAndServe(":8080", router.ChiMux))
}