2022-06-24 23:09:46 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2023-01-08 18:49:33 +04:00
|
|
|
|
2023-03-05 00:18:52 +04:00
|
|
|
"git.arav.su/Arav/justcaptcha/internal/http"
|
2022-06-24 23:09:46 +04:00
|
|
|
)
|
|
|
|
|
2022-08-17 21:49:57 +04:00
|
|
|
var version string
|
|
|
|
|
2023-01-08 17:04:20 +04:00
|
|
|
var listenAddress *string = flag.String("listen", "/var/run/justcaptcha/sock", "listen address (ip:port|unix_path)")
|
|
|
|
var captchaExpiry *time.Duration = flag.Duration("expiry", 10*time.Minute, "CAPTCHA expiry in format XX{s,m,h}, e.g. 5m, 300s")
|
2022-06-24 23:09:46 +04:00
|
|
|
var showVersion *bool = flag.Bool("v", false, "show version")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
|
|
|
if *showVersion {
|
2023-03-05 00:18:52 +04:00
|
|
|
fmt.Println("justcaptchad ver.", version, "\nCopyright (c) 2022 Alexander \"Arav\" Andreev <me@arav.su>")
|
2022-06-24 23:09:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:49:03 +04:00
|
|
|
hand := http.NewCaptchaHandlers(*captchaExpiry)
|
|
|
|
srv := http.NewHttpServer()
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:50:49 +04:00
|
|
|
srv.POST("/", hand.New)
|
2022-06-24 23:09:46 +04:00
|
|
|
srv.POST("/:captcha", hand.Solve)
|
|
|
|
srv.GET("/:captcha", hand.IsSolved)
|
|
|
|
srv.GET("/:captcha/image", hand.Image)
|
|
|
|
|
|
|
|
var network string
|
|
|
|
if !strings.ContainsRune(*listenAddress, ':') {
|
|
|
|
network = "unix"
|
2022-06-26 14:54:11 +04:00
|
|
|
defer os.Remove(*listenAddress)
|
2022-06-24 23:09:46 +04:00
|
|
|
} else {
|
|
|
|
ap, err := netip.ParseAddrPort(*listenAddress)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ap.Addr().Is4() {
|
|
|
|
network = "tcp4"
|
|
|
|
} else if ap.Addr().Is6() {
|
|
|
|
network = "tcp6"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.Start(network, *listenAddress); err != nil {
|
|
|
|
log.Fatalln("failed to start a server:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doneSignal := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
<-doneSignal
|
|
|
|
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
log.Fatalln("failed to properly shutdown a server:", err)
|
|
|
|
}
|
|
|
|
}
|