From 69f7951960e2899e7360bce3ae5f0663e12ac66d Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sun, 7 May 2017 15:51:37 -0400 Subject: [PATCH] add initial optional i2p connectivity --- config/config.go | 6 ++++-- config/i2p.go | 8 ++++++++ main.go | 13 +++++++------ network/network.go | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 config/i2p.go create mode 100644 network/network.go diff --git a/config/config.go b/config/config.go index b5a19cf2..56fe4699 100644 --- a/config/config.go +++ b/config/config.go @@ -17,9 +17,11 @@ type Config struct { // This will be directly passed to Gorm, and its internal // structure depends on the dialect for each db type DBParams string `json: "db_params"` + // optional i2p configuration + I2P *I2PConfig `json: "i2p"` } -var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50"} +var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", nil} var allowedDatabaseTypes = map[string]bool{ "sqlite3": true, @@ -102,4 +104,4 @@ func (config *Config) Pretty(output io.Writer) error { data = append(data, []byte("\n")...) _, err = output.Write(data) return err -} \ No newline at end of file +} diff --git a/config/i2p.go b/config/i2p.go new file mode 100644 index 00000000..60f0f966 --- /dev/null +++ b/config/i2p.go @@ -0,0 +1,8 @@ +package config + +// i2p connectivity config +type I2PConfig struct { + Name string `json: "name"` + Addr string `json: "samaddr"` + Keyfile string `json: "keyfile"` +} diff --git a/main.go b/main.go index 756b0a9f..f697cc9d 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,11 @@ package main import ( "bufio" "flag" - "fmt" "github.com/nicksnyder/go-i18n/i18n" "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" + "github.com/ewhal/nyaa/network" "github.com/ewhal/nyaa/router" "github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/signals" @@ -26,16 +26,17 @@ func RunServer(conf *config.Config) { http.Handle("/", router.Router) // Set up server, - addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) srv := &http.Server{ - Addr: addr, WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } - log.Infof("listening on %s", addr) - - err := srv.ListenAndServe() + l, err := network.CreateHTTPListener(conf) log.CheckError(err) + if err == nil { + log.Infof("listening on %s", l.Addr()) + err := srv.Serve(l) + log.CheckError(err) + } } func main() { diff --git a/network/network.go b/network/network.go new file mode 100644 index 00000000..89b249b6 --- /dev/null +++ b/network/network.go @@ -0,0 +1,24 @@ +package network + +import ( + "fmt" + "github.com/ewhal/nyaa/config" + "github.com/ewhal/nyaa/util/log" + "github.com/majestrate/i2p-tools/lib/i2p" + "net" +) + +// CreateHTTPListener creates a net.Listener for main http webapp given main config +func CreateHTTPListener(conf *config.Config) (l net.Listener, err error) { + if conf.I2P == nil { + l, err = net.Listen("tcp", fmt.Sprintf("%s:%d", conf.Host, conf.Port)) + } else { + s := i2p.NewSession(conf.I2P.Name, conf.I2P.Addr, conf.I2P.Keyfile) + err = s.Open() + if s != nil { + log.Infof("i2p address: %s", s.B32Addr()) + l = s + } + } + return +}