Skip to content

Commit

Permalink
Add ability to require encrypted peers
Browse files Browse the repository at this point in the history
Closes #5.
  • Loading branch information
FIGBERT committed Jun 26, 2022
1 parent 23490e7 commit f79a8f5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ port = 126

# Toggle client logging (logs are written to $XDG_STATE_HOME/mabel)
log = false

# Mabel always prefers encrypted connections. If set to true,
# require_encryption will have Mabel ignore unencrypted peers.
require_encryption = false
```

#### Theme
Expand Down
7 changes: 5 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ var (
type config struct {
Download string
Port uint
Log bool
Theme toml.Primitive

Log bool
RequireEncryption bool `toml:"require_encryption"`

Theme toml.Primitive
}

// getTheme checks the config file for a configured theme key and
Expand Down
4 changes: 2 additions & 2 deletions full/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (m model) Init() tea.Cmd {

// Execute creates the initial model and a Bubble Tea program, and
// aborts the client if that fails.
func Execute(torrents *[]string, dir *string, port *uint, logging *bool, theme *styles.ColorTheme) {
model, err := initialModel(torrents, dir, port, logging, theme)
func Execute(torrents *[]string, dir *string, port *uint, logging, encrypt *bool, theme *styles.ColorTheme) {
model, err := initialModel(torrents, dir, port, logging, encrypt, theme)
if err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 6 additions & 3 deletions full/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type model struct {
startupTorrents *[]string
dir *string
logging *bool
encrypt *bool
theme *styles.ColorTheme

client *torrent.Client
Expand All @@ -34,12 +35,13 @@ type model struct {

// genMabelConfig configures the torrent client (seeding, listening
// port, log directory, etc.)
func genMabelConfig(port *uint, logging *bool) *torrent.ClientConfig {
func genMabelConfig(port *uint, logging, encrypt *bool) *torrent.ClientConfig {
config := torrent.NewDefaultClientConfig()
config.Logger = log.Default
config.Logger.Handlers = []log.Handler{log.DiscardHandler}
config.Seed = true
config.ListenPort = int(*port)
config.HeaderObfuscationPolicy.RequirePreferred = *encrypt

if *logging {
config.Debug = true
Expand Down Expand Up @@ -74,15 +76,16 @@ func genList() *clist.Model {
}

// initialModel creates the model for the full client.
func initialModel(torrents *[]string, dir *string, port *uint, logging *bool, theme *styles.ColorTheme) (tea.Model, error) {
config := genMabelConfig(port, logging)
func initialModel(torrents *[]string, dir *string, port *uint, logging, encrypt *bool, theme *styles.ColorTheme) (tea.Model, error) {
config := genMabelConfig(port, logging, encrypt)
client, err := torrent.NewClient(config)
hlp := help.New()

m := model{
startupTorrents: torrents,
dir: dir,
logging: logging,
encrypt: encrypt,
theme: theme,

client: client,
Expand Down
2 changes: 1 addition & 1 deletion full/port_startup_failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m portStartupFailure) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
port := uint(prt)

config := genMabelConfig(&port, m.main.logging)
config := genMabelConfig(&port, m.main.logging, m.main.encrypt)
client, err := torrent.NewClient(config)
if err != nil {
return m, reportError(err)
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
port = flag.UintP("port", "p", 42069, "Set the port number to which the client will bind.")
theme = flag.StringP("theme", "t", "default", "Set the color theme that the client will use.")
logging = flag.BoolP("log", "l", false, "Enable client logging.")
encrypt = flag.BoolP("encrypt", "e", false, "Ignore unencrypted peers.")
help = flag.BoolP("help", "h", false, "Print this help message.")
vrsn = flag.BoolP("version", "v", false, "Print version information.")
)
Expand All @@ -56,6 +57,8 @@ func main() {
menu.WriteString("\n Set the color theme that the client will use. [default: " + green.Render("default") + "]")
menu.WriteString("\n " + green.Render("-l") + ", " + green.Render("--log"))
menu.WriteString("\n Enable client logging. [dir: " + green.Render("$XDG_STATE_HOME/mabel") + "]")
menu.WriteString("\n " + green.Render("-e") + ", " + green.Render("--encrypt"))
menu.WriteString("\n Ignore unencrypted peers.")
menu.WriteString("\n " + green.Render("-h") + ", " + green.Render("--help"))
menu.WriteString("\n Print this help message.")
menu.WriteString("\n " + green.Render("-v") + ", " + green.Render("--version"))
Expand Down Expand Up @@ -111,6 +114,7 @@ func main() {
portFlag := flag.Lookup("port")
themeFlag := flag.Lookup("theme")
loggingFlag := flag.Lookup("log")
encryptFlag := flag.Lookup("encrypt")
thm := conf.getTheme()

if !downloadFlag.Changed && conf.Download != "" {
Expand All @@ -122,15 +126,18 @@ func main() {
if !loggingFlag.Changed {
flag.Set("log", fmt.Sprint(conf.Log))
}
if !encryptFlag.Changed {
flag.Set("encrypt", fmt.Sprint(conf.RequireEncryption))
}
if themeFlag.Changed {
thm = styles.StringToTheme(theme)
}

styles.BorderWindow = styles.BorderWindow.BorderForeground(thm.Primary)

if flag.NArg() == 1 {
mini.Execute(&args[0], download, port, logging, thm)
mini.Execute(&args[0], download, port, logging, encrypt, thm)
} else {
full.Execute(&args, download, port, logging, thm)
full.Execute(&args, download, port, logging, encrypt, thm)
}
}
11 changes: 6 additions & 5 deletions mini/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ type model struct {

// genMabelConfig configures the torrent client (seeding, listening
// port, log directory, etc.)
func genMabelConfig(port *uint, logging *bool) *torrent.ClientConfig {
func genMabelConfig(port *uint, logging, encrypt *bool) *torrent.ClientConfig {
config := torrent.NewDefaultClientConfig()
config.Logger = log.Default
config.Logger.Handlers = []log.Handler{log.DiscardHandler}
config.Seed = true
config.ListenPort = int(*port)
config.HeaderObfuscationPolicy.RequirePreferred = *encrypt

if *logging {
config.Debug = true
Expand All @@ -65,8 +66,8 @@ func genMabelConfig(port *uint, logging *bool) *torrent.ClientConfig {

// initialModel creates the model for the mini client. If the torrent
// cannot be generated, the client aborts.
func initialModel(t, dir *string, port *uint, logging *bool, theme *styles.ColorTheme) (model, error) {
client, err := torrent.NewClient(genMabelConfig(port, logging))
func initialModel(t, dir *string, port *uint, logging, encrypt *bool, theme *styles.ColorTheme) (model, error) {
client, err := torrent.NewClient(genMabelConfig(port, logging, encrypt))
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -149,8 +150,8 @@ func (m model) View() string {

// Execute creates the initial model and a Bubble Tea program, and
// aborts the client if that fails.
func Execute(t, dir *string, port *uint, logging *bool, theme *styles.ColorTheme) {
model, err := initialModel(t, dir, port, logging, theme)
func Execute(t, dir *string, port *uint, logging, encrypt *bool, theme *styles.ColorTheme) {
model, err := initialModel(t, dir, port, logging, encrypt, theme)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit f79a8f5

Please sign in to comment.