2022-12-06 20:20:27 +00:00
|
|
|
package main
|
|
|
|
|
2022-12-06 22:10:27 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-12-06 22:25:45 +00:00
|
|
|
"os/signal"
|
2022-12-06 22:10:27 +00:00
|
|
|
"strconv"
|
2022-12-06 22:25:45 +00:00
|
|
|
"time"
|
2022-12-06 22:10:27 +00:00
|
|
|
|
2022-12-06 22:25:45 +00:00
|
|
|
"github.com/eyedeekay/onramp"
|
2022-12-06 22:10:27 +00:00
|
|
|
server "i2pgit.org/idk/newsgo/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
serve = flag.String("command", "help", "command to run(may be `serve`,`build`,`sign`")
|
|
|
|
dir = flag.String("newsdir", "build", "directory to serve news from")
|
|
|
|
statsfile = flag.String("statsfile", "build/stats.json", "file to store stats in")
|
|
|
|
host = flag.String("host", "127.0.0.1", "host to serve on")
|
|
|
|
port = flag.String("port", "9696", "port to serve on")
|
2022-12-06 23:23:18 +00:00
|
|
|
i2p = flag.Bool("i2p", true, "automatically co-host on an I2P service using SAMv3")
|
|
|
|
tcp = flag.Bool("http", true, "host on an HTTP service at host:port")
|
2022-12-06 22:10:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func validatePort(s *string) {
|
|
|
|
_, err := strconv.Atoi(*s)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Port is invalid")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateCommand(s *string) string {
|
|
|
|
switch *s {
|
|
|
|
case "serve":
|
|
|
|
return "serve"
|
|
|
|
case "build":
|
|
|
|
return "build"
|
|
|
|
case "sign":
|
|
|
|
return "sign"
|
|
|
|
default:
|
|
|
|
return "help"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Help() {
|
|
|
|
fmt.Println("newsgo")
|
|
|
|
fmt.Println("======")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("I2P News Server Tool/Library")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("Usage")
|
|
|
|
fmt.Println("-----")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("./newsgo -command $command -newsdir $news_directory -statsfile $news_stats_file")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("### Commands")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println(" - serve: Serve newsfeeds from a directory")
|
|
|
|
fmt.Println(" - build: Build newsfeeds from XML(Not Implemented Yet)")
|
|
|
|
fmt.Println(" - sign: Sign newsfeeds with local keys(Not Implemented Yet)")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("### Options")
|
|
|
|
fmt.Println("")
|
2022-12-06 22:13:57 +00:00
|
|
|
fmt.Println("Use these options to configure the software")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("#### Server Options(use with `serve`")
|
|
|
|
fmt.Println("")
|
2022-12-06 22:10:27 +00:00
|
|
|
fmt.Println(" - `-newsdir`: directory to serve newsfeed from")
|
|
|
|
fmt.Println(" - `-statsfile`: file to store the stats in, in json format")
|
2022-12-06 22:13:57 +00:00
|
|
|
fmt.Println(" - `-host`: host to serve news files on")
|
|
|
|
fmt.Println(" - `-port`: port to serve news files on")
|
|
|
|
fmt.Println(" - `-i2p`: serve news files directly to I2P using SAMv3")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("#### Builder Options(use with `build`")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("Not implemented yet")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("#### Signer Options(use with `sign`")
|
2022-12-06 22:10:27 +00:00
|
|
|
fmt.Println("")
|
2022-12-06 22:13:57 +00:00
|
|
|
fmt.Println("Not implemented yet")
|
2022-12-06 22:10:27 +00:00
|
|
|
}
|
2022-12-06 20:20:27 +00:00
|
|
|
|
2022-12-06 23:18:16 +00:00
|
|
|
func Serve(host, port string, s *server.NewsServer) error {
|
2022-12-06 22:10:27 +00:00
|
|
|
ln, err := net.Listen("tcp", net.JoinHostPort(host, port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-06 22:25:45 +00:00
|
|
|
return http.Serve(ln, s)
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:18:16 +00:00
|
|
|
func ServeI2P(s *server.NewsServer) error {
|
2022-12-06 22:25:45 +00:00
|
|
|
garlic := &onramp.Garlic{}
|
|
|
|
defer garlic.Close()
|
|
|
|
ln, err := garlic.Listen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
2022-12-06 22:10:27 +00:00
|
|
|
return http.Serve(ln, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
command := validateCommand(serve)
|
|
|
|
validatePort(port)
|
|
|
|
switch command {
|
|
|
|
case "serve":
|
2022-12-06 23:18:16 +00:00
|
|
|
s := server.Serve(*dir, *statsfile)
|
2022-12-06 23:23:18 +00:00
|
|
|
if *tcp {
|
|
|
|
go func() {
|
|
|
|
if err := Serve(*host, *port, s); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2022-12-06 22:25:45 +00:00
|
|
|
if *i2p {
|
|
|
|
go func() {
|
2022-12-06 23:18:16 +00:00
|
|
|
if err := ServeI2P(s); err != nil {
|
2022-12-06 22:25:45 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
for sig := range c {
|
|
|
|
log.Println("captured: ", sig)
|
2022-12-06 23:18:16 +00:00
|
|
|
s.Stats.Save()
|
2022-12-06 22:25:45 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Minute)
|
2022-12-06 23:18:16 +00:00
|
|
|
log.Printf("Running for %d minutes.", i)
|
2022-12-06 22:25:45 +00:00
|
|
|
i++
|
2022-12-06 22:10:27 +00:00
|
|
|
}
|
|
|
|
case "build":
|
|
|
|
case "sign":
|
|
|
|
case "help":
|
|
|
|
Help()
|
|
|
|
default:
|
|
|
|
Help()
|
|
|
|
}
|
2022-12-06 20:20:27 +00:00
|
|
|
}
|