Restructured source code layout, Added rice support for templates and static assets
This commit is contained in:
parent
76fa613b7e
commit
4dae45a68f
20 changed files with 133 additions and 228 deletions
8
Makefile
8
Makefile
|
@ -1,4 +1,4 @@
|
||||||
.PHONY: dev build install test release clean
|
.PHONY: dev setup build install test release clean
|
||||||
|
|
||||||
CGO_ENABLED=0
|
CGO_ENABLED=0
|
||||||
VERSION=$(shell git describe --abbrev=0 --tags)
|
VERSION=$(shell git describe --abbrev=0 --tags)
|
||||||
|
@ -9,8 +9,12 @@ all: dev
|
||||||
dev: build
|
dev: build
|
||||||
@./tube -v
|
@./tube -v
|
||||||
|
|
||||||
|
setup:
|
||||||
|
@go get github.com/GeertJohan/go.rice/rice
|
||||||
|
|
||||||
build: clean
|
build: clean
|
||||||
@go build \
|
@go generate $(shell go list)/...
|
||||||
|
@go build \
|
||||||
-tags "netgo static_build" -installsuffix netgo \
|
-tags "netgo static_build" -installsuffix netgo \
|
||||||
-ldflags "-w -X $(shell go list).Version=$(VERSION) -X $(shell go list).Commit=$(COMMIT)" \
|
-ldflags "-w -X $(shell go list).Version=$(VERSION) -X $(shell go list).Commit=$(COMMIT)" \
|
||||||
.
|
.
|
||||||
|
|
|
@ -8,7 +8,6 @@ Some of the key features include:
|
||||||
- No JavaScript (the player UI is entirely HTML)
|
- No JavaScript (the player UI is entirely HTML)
|
||||||
- Easy to customize CSS and HTML template
|
- Easy to customize CSS and HTML template
|
||||||
- Automatically generates RSS feed (at `/feed.xml`)
|
- Automatically generates RSS feed (at `/feed.xml`)
|
||||||
- Builtin Tor onion service support
|
|
||||||
- Clean, simple, familiar UI
|
- Clean, simple, familiar UI
|
||||||
|
|
||||||
Currently only supports MP4 video files so you may need to re-encode your media to MP4 using something like [ffmpeg](https://ffmpeg.org/).
|
Currently only supports MP4 video files so you may need to re-encode your media to MP4 using something like [ffmpeg](https://ffmpeg.org/).
|
||||||
|
|
|
@ -2,20 +2,20 @@
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
rice "github.com/GeertJohan/go.rice"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/wybiral/tube/pkg/media"
|
"github.com/wybiral/tube/media"
|
||||||
"github.com/wybiral/tube/pkg/onionkey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:generate rice embed-go
|
||||||
|
|
||||||
// App represents main application.
|
// App represents main application.
|
||||||
type App struct {
|
type App struct {
|
||||||
Config *Config
|
Config *Config
|
||||||
|
@ -23,7 +23,6 @@ type App struct {
|
||||||
Watcher *fsnotify.Watcher
|
Watcher *fsnotify.Watcher
|
||||||
Templates *template.Template
|
Templates *template.Template
|
||||||
Feed []byte
|
Feed []byte
|
||||||
Tor *tor
|
|
||||||
Listener net.Listener
|
Listener net.Listener
|
||||||
Router *mux.Router
|
Router *mux.Router
|
||||||
}
|
}
|
||||||
|
@ -51,15 +50,10 @@ func NewApp(cfg *Config) (*App, error) {
|
||||||
}
|
}
|
||||||
a.Listener = ln
|
a.Listener = ln
|
||||||
// Setup Templates
|
// Setup Templates
|
||||||
a.Templates = template.Must(template.ParseGlob("templates/*"))
|
box := rice.MustFindBox("../templates")
|
||||||
// Setup Tor
|
index := template.New("index")
|
||||||
if cfg.Tor.Enable {
|
a.Templates = template.Must(index.Parse(box.MustString("index.html")))
|
||||||
t, err := newTor(cfg.Tor)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
a.Tor = t
|
|
||||||
}
|
|
||||||
// Setup Router
|
// Setup Router
|
||||||
r := mux.NewRouter().StrictSlash(true)
|
r := mux.NewRouter().StrictSlash(true)
|
||||||
r.HandleFunc("/", a.indexHandler).Methods("GET")
|
r.HandleFunc("/", a.indexHandler).Methods("GET")
|
||||||
|
@ -72,8 +66,8 @@ func NewApp(cfg *Config) (*App, error) {
|
||||||
r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET")
|
r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET")
|
||||||
// Static file handler
|
// Static file handler
|
||||||
fsHandler := http.StripPrefix(
|
fsHandler := http.StripPrefix(
|
||||||
"/static/",
|
"/static",
|
||||||
http.FileServer(http.Dir("./static/")),
|
http.FileServer(rice.MustFindBox("../static").HTTPBox()),
|
||||||
)
|
)
|
||||||
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
|
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
|
||||||
a.Router = r
|
a.Router = r
|
||||||
|
@ -82,28 +76,6 @@ func NewApp(cfg *Config) (*App, error) {
|
||||||
|
|
||||||
// Run imports the library and starts server.
|
// Run imports the library and starts server.
|
||||||
func (a *App) Run() error {
|
func (a *App) Run() error {
|
||||||
if a.Tor != nil {
|
|
||||||
var err error
|
|
||||||
cs := a.Config.Server
|
|
||||||
key := a.Tor.OnionKey
|
|
||||||
if key == nil {
|
|
||||||
key, err = onionkey.GenerateKey()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.Tor.OnionKey = key
|
|
||||||
}
|
|
||||||
onion, err := key.Onion()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
onion.Ports[80] = fmt.Sprintf("%s:%d", cs.Host, cs.Port)
|
|
||||||
err = a.Tor.Controller.AddOnion(onion)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("unable to start Tor onion service")
|
|
||||||
}
|
|
||||||
log.Printf("Onion service: http://%s.onion", onion.ServiceID)
|
|
||||||
}
|
|
||||||
for _, pc := range a.Config.Library {
|
for _, pc := range a.Config.Library {
|
||||||
p := &media.Path{
|
p := &media.Path{
|
||||||
Path: pc.Path,
|
Path: pc.Path,
|
||||||
|
@ -131,7 +103,7 @@ func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if len(pl) > 0 {
|
if len(pl) > 0 {
|
||||||
http.Redirect(w, r, "/v/"+pl[0].ID, 302)
|
http.Redirect(w, r, "/v/"+pl[0].ID, 302)
|
||||||
} else {
|
} else {
|
||||||
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
a.Templates.ExecuteTemplate(w, "index", &struct {
|
||||||
Playing *media.Video
|
Playing *media.Video
|
||||||
Playlist media.Playlist
|
Playlist media.Playlist
|
||||||
}{
|
}{
|
||||||
|
@ -152,7 +124,7 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("/v/%s", id)
|
log.Printf("/v/%s", id)
|
||||||
playing, ok := a.Library.Videos[id]
|
playing, ok := a.Library.Videos[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
a.Templates.ExecuteTemplate(w, "index", &struct {
|
||||||
Playing *media.Video
|
Playing *media.Video
|
||||||
Playlist media.Playlist
|
Playlist media.Playlist
|
||||||
}{
|
}{
|
||||||
|
@ -162,7 +134,7 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
a.Templates.ExecuteTemplate(w, "index", &struct {
|
||||||
Playing *media.Video
|
Playing *media.Video
|
||||||
Playlist media.Playlist
|
Playlist media.Playlist
|
||||||
}{
|
}{
|
||||||
|
@ -207,7 +179,7 @@ func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
||||||
if m.ThumbType == "" {
|
if m.ThumbType == "" {
|
||||||
w.Header().Set("Content-Type", "image/jpeg")
|
w.Header().Set("Content-Type", "image/jpeg")
|
||||||
http.ServeFile(w, r, "static/defaulticon.jpg")
|
w.Write(rice.MustFindBox("../static").MustBytes("defaulticon.jpg"))
|
||||||
} else {
|
} else {
|
||||||
w.Header().Set("Content-Type", m.ThumbType)
|
w.Header().Set("Content-Type", m.ThumbType)
|
||||||
w.Write(m.Thumb)
|
w.Write(m.Thumb)
|
|
@ -10,7 +10,6 @@ type Config struct {
|
||||||
Library []*PathConfig `json:"library"`
|
Library []*PathConfig `json:"library"`
|
||||||
Server *ServerConfig `json:"server"`
|
Server *ServerConfig `json:"server"`
|
||||||
Feed *FeedConfig `json:"feed"`
|
Feed *FeedConfig `json:"feed"`
|
||||||
Tor *TorConfig `json:"tor,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PathConfig settings for media library path.
|
// PathConfig settings for media library path.
|
||||||
|
@ -38,19 +37,6 @@ type FeedConfig struct {
|
||||||
Copyright string `json:"copyright"`
|
Copyright string `json:"copyright"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TorConfig stores tor configuration.
|
|
||||||
type TorConfig struct {
|
|
||||||
Enable bool `json:"enable"`
|
|
||||||
Controller *TorControllerConfig `json:"controller"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// TorControllerConfig stores tor controller configuration.
|
|
||||||
type TorControllerConfig struct {
|
|
||||||
Host string `json:"host"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
Password string `json:"password,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultConfig returns Config initialized with default values.
|
// DefaultConfig returns Config initialized with default values.
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
|
@ -67,13 +53,6 @@ func DefaultConfig() *Config {
|
||||||
Feed: &FeedConfig{
|
Feed: &FeedConfig{
|
||||||
ExternalURL: "http://localhost",
|
ExternalURL: "http://localhost",
|
||||||
},
|
},
|
||||||
Tor: &TorConfig{
|
|
||||||
Enable: false,
|
|
||||||
Controller: &TorControllerConfig{
|
|
||||||
Host: "127.0.0.1",
|
|
||||||
Port: 9051,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,12 +29,6 @@ func buildFeed(a *App) {
|
||||||
var externalURL string
|
var externalURL string
|
||||||
if len(cfg.ExternalURL) > 0 {
|
if len(cfg.ExternalURL) > 0 {
|
||||||
externalURL = cfg.ExternalURL
|
externalURL = cfg.ExternalURL
|
||||||
} else if a.Tor != nil {
|
|
||||||
onion, err := a.Tor.OnionKey.Onion()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
externalURL = fmt.Sprintf("http://%s.onion", onion.ServiceID)
|
|
||||||
} else {
|
} else {
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
95
app/rice-box.go
Normal file
95
app/rice-box.go
Normal file
File diff suppressed because one or more lines are too long
|
@ -19,12 +19,5 @@
|
||||||
"email": "author@somewhere.example"
|
"email": "author@somewhere.example"
|
||||||
},
|
},
|
||||||
"copyright": "Copyright Text"
|
"copyright": "Copyright Text"
|
||||||
},
|
|
||||||
"tor": {
|
|
||||||
"enable": false,
|
|
||||||
"controller": {
|
|
||||||
"host": "127.0.0.1",
|
|
||||||
"port": 9051
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -3,6 +3,7 @@ module github.com/wybiral/tube
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/GeertJohan/go.rice v1.0.0
|
||||||
github.com/dhowden/tag v0.0.0-20190519100835-db0c67e351b1
|
github.com/dhowden/tag v0.0.0-20190519100835-db0c67e351b1
|
||||||
github.com/fsnotify/fsnotify v1.4.7
|
github.com/fsnotify/fsnotify v1.4.7
|
||||||
github.com/gorilla/feeds v1.1.1
|
github.com/gorilla/feeds v1.1.1
|
||||||
|
@ -10,7 +11,6 @@ require (
|
||||||
github.com/sirupsen/logrus v1.4.2
|
github.com/sirupsen/logrus v1.4.2
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/wybiral/feeds v1.1.1
|
github.com/wybiral/feeds v1.1.1
|
||||||
github.com/wybiral/torgo v0.0.0-20190413024533-a19a6c8a5048
|
|
||||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
|
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
|
||||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7
|
golang.org/x/net v0.0.0-20190628185345-da137c7871d7
|
||||||
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb
|
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb
|
||||||
|
|
16
go.sum
16
go.sum
|
@ -1,3 +1,11 @@
|
||||||
|
github.com/GeertJohan/go.incremental v1.0.0 h1:7AH+pY1XUgQE4Y1HcXYaMqAI0m9yrFqo/jt0CW30vsg=
|
||||||
|
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
|
||||||
|
github.com/GeertJohan/go.rice v1.0.0 h1:KkI6O9uMaQU3VEKaj01ulavtF7o1fWT7+pk/4voiMLQ=
|
||||||
|
github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0=
|
||||||
|
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
|
||||||
|
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
|
||||||
|
github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY=
|
||||||
|
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dhowden/tag v0.0.0-20190519100835-db0c67e351b1 h1:HR8W6GvuS20j4kNxa/XQeyVA0vHLKVMCAVJj0RGWauY=
|
github.com/dhowden/tag v0.0.0-20190519100835-db0c67e351b1 h1:HR8W6GvuS20j4kNxa/XQeyVA0vHLKVMCAVJj0RGWauY=
|
||||||
github.com/dhowden/tag v0.0.0-20190519100835-db0c67e351b1/go.mod h1:SniNVYuaD1jmdEEvi+7ywb1QFR7agjeTdGKyFb0p7Rw=
|
github.com/dhowden/tag v0.0.0-20190519100835-db0c67e351b1/go.mod h1:SniNVYuaD1jmdEEvi+7ywb1QFR7agjeTdGKyFb0p7Rw=
|
||||||
|
@ -6,7 +14,11 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
|
||||||
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
|
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
|
||||||
github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I=
|
github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I=
|
||||||
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||||
|
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
|
||||||
|
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229 h1:E2B8qYyeSgv5MXpmzZXRNp8IAQ4vjxIjhpAf5hv/tAg=
|
||||||
|
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
|
@ -14,6 +26,10 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
|
||||||
|
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||||
github.com/wybiral/feeds v1.1.1 h1:KWE/JxA2XfP0My+C0wymqXrWK5oIyjRVbH5kpclsea0=
|
github.com/wybiral/feeds v1.1.1 h1:KWE/JxA2XfP0My+C0wymqXrWK5oIyjRVbH5kpclsea0=
|
||||||
github.com/wybiral/feeds v1.1.1/go.mod h1:MRSqtY+Oy5HMM51cF212xqU39MYbsYq/AH+PY8IfeM0=
|
github.com/wybiral/feeds v1.1.1/go.mod h1:MRSqtY+Oy5HMM51cF212xqU39MYbsYq/AH+PY8IfeM0=
|
||||||
github.com/wybiral/torgo v0.0.0-20190413024533-a19a6c8a5048 h1:HQmSLHtGgSARs7VYzAID703qM/5m1il3X7f9AHfg/9c=
|
github.com/wybiral/torgo v0.0.0-20190413024533-a19a6c8a5048 h1:HQmSLHtGgSARs7VYzAID703qM/5m1il3X7f9AHfg/9c=
|
||||||
|
|
2
main.go
2
main.go
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
"github.com/wybiral/tube/pkg/app"
|
"github.com/wybiral/tube/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -1,45 +0,0 @@
|
||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/wybiral/torgo"
|
|
||||||
"github.com/wybiral/tube/pkg/onionkey"
|
|
||||||
)
|
|
||||||
|
|
||||||
type tor struct {
|
|
||||||
OnionKey onionkey.Key
|
|
||||||
Controller *torgo.Controller
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTor(ct *TorConfig) (*tor, error) {
|
|
||||||
addr := fmt.Sprintf("%s:%d", ct.Controller.Host, ct.Controller.Port)
|
|
||||||
ctrl, err := torgo.NewController(addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("unable to connect to Tor controller")
|
|
||||||
}
|
|
||||||
if len(ct.Controller.Password) > 0 {
|
|
||||||
err = ctrl.AuthenticatePassword(ct.Controller.Password)
|
|
||||||
} else {
|
|
||||||
err = ctrl.AuthenticateCookie()
|
|
||||||
if err != nil {
|
|
||||||
err = ctrl.AuthenticateNone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("unable to authenticate to Tor controller")
|
|
||||||
}
|
|
||||||
key, err := onionkey.ReadFile("onion.key")
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
key = nil
|
|
||||||
} else if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
t := &tor{
|
|
||||||
Controller: ctrl,
|
|
||||||
OnionKey: key,
|
|
||||||
}
|
|
||||||
return t, nil
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
// Package onionkey manages onion service key generation, serialization, and
|
|
||||||
// service ID calculation. Currently only supports version 3 onions.
|
|
||||||
package onionkey
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/wybiral/torgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Key is generic interface type for Tor onion keys.
|
|
||||||
type Key interface {
|
|
||||||
WriteFile(path string) error
|
|
||||||
Onion() (*torgo.Onion, error)
|
|
||||||
ServiceID() string
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateKey generates a Tor onion key.
|
|
||||||
func GenerateKey() (Key, error) {
|
|
||||||
return generateV3()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadFile reads a Tor onion key from file path.
|
|
||||||
func ReadFile(path string) (Key, error) {
|
|
||||||
return readV3(path)
|
|
||||||
}
|
|
|
@ -1,78 +0,0 @@
|
||||||
// Implements Tor v3 onion key based on ed25519.
|
|
||||||
|
|
||||||
package onionkey
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"encoding/base32"
|
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/wybiral/torgo"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
)
|
|
||||||
|
|
||||||
type v3Key ed25519.PrivateKey
|
|
||||||
|
|
||||||
func generateV3() (v3Key, error) {
|
|
||||||
_, key, err := ed25519.GenerateKey(rand.Reader)
|
|
||||||
return v3Key(key), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func readV3(path string) (v3Key, error) {
|
|
||||||
raw, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
pk := strings.TrimSpace(string(raw))
|
|
||||||
parts := strings.SplitN(pk, ":", 2)
|
|
||||||
if parts[0] != "v3" {
|
|
||||||
return nil, errors.New("Invalid key type")
|
|
||||||
}
|
|
||||||
seed, err := base64.StdEncoding.DecodeString(parts[1])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
key := ed25519.NewKeyFromSeed(seed)
|
|
||||||
return v3Key(key), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k v3Key) Onion() (*torgo.Onion, error) {
|
|
||||||
return torgo.OnionFromEd25519(ed25519.PrivateKey(k))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k v3Key) WriteFile(path string) error {
|
|
||||||
seed := ed25519.PrivateKey(k).Seed()
|
|
||||||
b64 := base64.StdEncoding.EncodeToString(seed)
|
|
||||||
f, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
_, err = f.WriteString("v3:" + b64)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k v3Key) ServiceID() string {
|
|
||||||
// Get ed25519 public key
|
|
||||||
pub := ed25519.PrivateKey(k).Public().(ed25519.PublicKey)
|
|
||||||
// Calculate check digits
|
|
||||||
checkstr := []byte(".onion checksum")
|
|
||||||
checkstr = append(checkstr, pub...)
|
|
||||||
checkstr = append(checkstr, 0x03)
|
|
||||||
checksum := sha3.Sum256(checkstr)
|
|
||||||
checkdigits := checksum[:2]
|
|
||||||
// Calculate service ID
|
|
||||||
combined := pub[:]
|
|
||||||
combined = append(combined, checkdigits...)
|
|
||||||
combined = append(combined, 0x03)
|
|
||||||
serviceID := base32.StdEncoding.EncodeToString(combined)
|
|
||||||
return strings.ToLower(serviceID)
|
|
||||||
}
|
|
Loading…
Reference in a new issue