2020-09-06 18:41:21 -04:00
package main
import (
"flag"
"fmt"
2020-09-15 01:03:45 -04:00
"net"
2020-09-10 17:40:37 -04:00
//"io/ioutil"
2020-09-06 18:41:21 -04:00
"log"
2020-09-15 01:03:45 -04:00
"os"
2020-09-06 18:41:21 -04:00
//"strings"
2020-10-25 01:55:58 -04:00
"path/filepath"
2020-09-06 18:41:21 -04:00
"time"
"github.com/eyedeekay/checki2cp"
"github.com/eyedeekay/checki2cp/controlcheck"
2020-09-15 01:03:45 -04:00
"github.com/eyedeekay/di2prc/lib"
2020-09-06 18:41:21 -04:00
"github.com/eyedeekay/go-i2pcontrol"
"github.com/eyedeekay/i2p-traymenu/icon"
2020-10-25 01:55:58 -04:00
"github.com/eyedeekay/i2p-traymenu/irc"
2020-09-06 18:41:21 -04:00
"github.com/eyedeekay/i2pbrowser/import"
2020-10-25 01:55:58 -04:00
2020-09-15 01:03:45 -04:00
Core "github.com/eyedeekay/opentracker"
"github.com/eyedeekay/sam3/i2pkeys"
2020-09-10 17:40:37 -04:00
"github.com/eyedeekay/toopie.html/import"
2020-09-06 18:41:21 -04:00
"github.com/getlantern/systray"
2020-09-15 01:03:45 -04:00
"github.com/vvampirius/retracker/core/common"
2020-09-06 18:41:21 -04:00
)
var usage = ` i2p - traymenu
== == == == == =
Tray interface to monitor and manage I2P router service . Basically , a
2020-10-25 01:55:58 -04:00
tray i2pcontrol client . Also has an embedded IRC client .
2020-09-06 18:41:21 -04:00
`
// -block default:false
2020-10-25 01:55:58 -04:00
var home , _ = os . UserHomeDir ( )
2020-09-06 18:41:21 -04:00
var (
2020-09-15 01:03:45 -04:00
host = flag . String ( "host" , "localhost" , "Host of the i2pcontrol and SAM interfaces" )
2020-09-06 18:41:21 -04:00
port = flag . String ( "port" , "7657" , "Port of the i2pcontrol interface" )
2020-10-25 01:55:58 -04:00
dir = flag . String ( "dir" , filepath . Join ( home , "i2p/opt/native-traymenu" ) , "Path to the configuration directory" )
2020-09-06 18:41:21 -04:00
path = flag . String ( "path" , "jsonrpc" , "Path to the i2pcontrol interface" )
password = flag . String ( "password" , "itoopie" , "Password for the i2pcontrol interface" )
shelp = flag . Bool ( "h" , false , "Show the help message" )
lhelp = flag . Bool ( "help" , false , "Show the help message" )
2020-09-15 01:03:45 -04:00
debug = flag . Bool ( "d" , false , "Debug mode" )
2020-10-25 01:55:58 -04:00
ot = flag . Bool ( "tracker" , false , "Run an open torrent tracker" )
chat = flag . Bool ( "irc" , true , "Run an IRC client connected to I2P" )
2020-09-15 01:03:45 -04:00
age = flag . Float64 ( "a" , 1800 , "Keep 'n' minutes peer in memory" )
sam = flag . String ( "sam" , "7656" , "Port of the SAMv3 interface, host must match i2pcontrol" )
2020-09-06 18:41:21 -04:00
// block = flag.Bool("block", false, "Block the terminal until the router is completely shut down")
)
2020-09-15 01:03:45 -04:00
var di2prcln net . Listener
2020-09-06 18:41:21 -04:00
func main ( ) {
flag . Parse ( )
if * shelp || * lhelp {
fmt . Printf ( usage )
2020-10-25 01:55:58 -04:00
flag . PrintDefaults ( )
2020-09-06 18:41:21 -04:00
return
}
2020-10-25 01:55:58 -04:00
if * chat {
go trayirc . IRC ( * dir )
2020-11-09 00:45:20 -05:00
go trayirc . IRCServerMain ( * dir )
2020-10-25 01:55:58 -04:00
}
if * ot {
go tracker ( )
}
2020-09-15 01:03:45 -04:00
di2prcln = di2prc . Listen ( * host + ":" + * sam , "" , "" )
2020-09-06 18:41:21 -04:00
onExit := func ( ) {
2020-09-15 01:03:45 -04:00
defer di2prcln . Close ( )
2020-09-07 00:26:42 -04:00
log . Println ( "Exiting now." )
2020-09-06 18:41:21 -04:00
}
systray . Run ( onReady , onExit )
}
2020-09-15 01:03:45 -04:00
func tracker ( ) {
flag . Usage = func ( ) {
fmt . Fprintf ( os . Stderr , "Usage of %s:\n" , os . Args [ 0 ] )
flag . PrintDefaults ( )
}
flag . Parse ( )
config := common . Config {
Listen : "127.0.0.1:80" ,
Debug : * debug ,
Age : * age ,
XRealIP : false ,
}
Core . New ( & config )
}
2020-09-06 18:41:21 -04:00
func onReady ( ) {
2020-09-15 01:03:45 -04:00
systray . SetTemplateIcon ( icon . Icon , icon . Icon )
2020-09-06 18:41:21 -04:00
systray . SetTitle ( "I2P Controller" )
systray . SetTooltip ( "Freestanding Invisisble Internet Router Control Appliance" )
mStartOrig := systray . AddMenuItem ( "Start I2P" , "Start the I2P Service" )
mStopOrig := systray . AddMenuItem ( "Stop I2P" , "Stop the I2P Service" )
mRestartOrig := systray . AddMenuItem ( "Restart I2P" , "Restart the I2P Service" )
2020-09-15 01:03:45 -04:00
systray . AddSeparator ( )
2020-09-06 18:41:21 -04:00
mBrowseOrig := systray . AddMenuItem ( "Launch an I2P Browser" , "Start an available browser, configured for I2P" )
2020-09-15 01:03:45 -04:00
subMenuTop := systray . AddMenuItem ( "I2P Applications" , "I2P Applications" )
smConsole := subMenuTop . AddSubMenuItem ( "I2P Router Console" , "Go to the I2P config page" )
smTorrent := subMenuTop . AddSubMenuItem ( "Bittorrent" , "Manage your Bittorrent Client" )
smEmail := subMenuTop . AddSubMenuItem ( "Mail" , "Send and Recieve email" )
smServices := subMenuTop . AddSubMenuItem ( "Hidden Services Mangager" , "Set up and tear down tunnels" )
smDNS := subMenuTop . AddSubMenuItem ( "Address Book" , "Store contact addresses" )
2020-10-25 02:22:57 -04:00
mIRC := subMenuTop . AddSubMenuItem ( "IRC Chat" , "Talk to others on I2P IRC" )
2020-09-15 01:03:45 -04:00
mChatOrig := systray . AddMenuItem ( "Distributed Chat" , "(Experimental) Distributed group-chat" )
mStatOrig := systray . AddMenuItem ( "I2P Router Stats" , "View I2P Router Console Statistics" )
systray . AddSeparator ( )
2020-09-06 18:41:21 -04:00
mQuitOrig := systray . AddMenuItem ( "Close Tray" , "Close the tray app, but don't shutdown the router" )
mWarnOrig := systray . AddMenuItem ( "I2P is Running but I2PControl is Not available.\nEnable jsonrpc on your I2P router." , "Warn the user if functionality is limited." )
2020-09-15 01:03:45 -04:00
sub := true
2020-09-06 18:41:21 -04:00
go func ( ) {
<- mQuitOrig . ClickedCh
systray . Quit ( )
} ( )
2020-09-15 01:03:45 -04:00
smConsole . Hide ( )
smTorrent . Hide ( )
smEmail . Hide ( )
smServices . Hide ( )
smDNS . Hide ( )
2020-10-25 02:22:57 -04:00
mChatOrig . Hide ( )
2020-09-06 18:41:21 -04:00
refreshStart := func ( ) {
ok , err := checki2p . CheckI2PIsRunning ( )
if err != nil {
}
if ok {
mStartOrig . Hide ( )
mBrowseOrig . Show ( )
} else {
mStartOrig . Show ( )
mBrowseOrig . Hide ( )
}
}
refreshStart ( )
go func ( ) {
for {
go func ( ) {
<- mStartOrig . ClickedCh
checki2p . ConditionallyLaunchI2P ( )
} ( )
2020-09-15 01:03:45 -04:00
go func ( ) {
<- subMenuTop . ClickedCh
if sub {
smConsole . Show ( )
smTorrent . Show ( )
smEmail . Show ( )
smServices . Show ( )
smDNS . Show ( )
t := sub
sub = ! t
} else {
smConsole . Hide ( )
smTorrent . Hide ( )
smEmail . Hide ( )
smServices . Hide ( )
smDNS . Hide ( )
t := sub
sub = ! t
}
} ( )
go func ( ) {
<- smConsole . ClickedCh
2020-10-25 01:55:58 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "--app" , "http://127.0.0.1:7657/console" } )
2020-09-15 01:03:45 -04:00
} ( )
go func ( ) {
<- smTorrent . ClickedCh
2020-10-25 01:55:58 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "--app" , "http://127.0.0.1:7657/i2psnark/" } )
2020-09-15 01:03:45 -04:00
} ( )
go func ( ) {
<- smEmail . ClickedCh
2020-10-25 01:55:58 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "--app" , "http://127.0.0.1:7657/susimail/" } )
2020-09-15 01:03:45 -04:00
} ( )
go func ( ) {
<- smServices . ClickedCh
2020-10-25 01:55:58 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "--app" , "http://127.0.0.1:7657/i2ptunnel/" } )
2020-09-15 01:03:45 -04:00
} ( )
go func ( ) {
<- smDNS . ClickedCh
2020-10-25 01:55:58 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "--app" , "http://127.0.0.1:7657/susidns/" } )
2020-09-15 01:03:45 -04:00
} ( )
2020-10-25 02:22:57 -04:00
go func ( ) {
<- mIRC . ClickedCh
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "--app" , "http://127.0.0.1:7669/connect" } )
} ( )
2020-09-06 18:41:21 -04:00
go func ( ) {
<- mBrowseOrig . ClickedCh
log . Println ( "Launching an I2P Browser" )
2020-09-15 01:03:45 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( nil )
2020-09-06 18:41:21 -04:00
} ( )
2020-09-10 17:40:37 -04:00
go func ( ) {
<- mStatOrig . ClickedCh
log . Println ( "Launching toopie.html" )
go toopiexec . Run ( )
} ( )
2020-09-15 01:03:45 -04:00
go func ( ) {
<- mChatOrig . ClickedCh
log . Println ( "Launching di2prc" )
2020-10-25 01:55:58 -04:00
go i2pbrowser . MainNoEmbeddedStuff ( [ ] string { "about:blank" , "http://" + di2prcln . Addr ( ) . ( i2pkeys . I2PAddr ) . Base32 ( ) } )
2020-09-15 01:03:45 -04:00
} ( )
2020-09-06 18:41:21 -04:00
go func ( ) {
<- mStopOrig . ClickedCh
log . Println ( "Beginning to shutdown I2P" )
i2pcontrol . ShutdownGraceful ( )
refreshStart ( )
} ( )
go func ( ) {
<- mRestartOrig . ClickedCh
log . Println ( "Beginning to restart I2P" )
i2pcontrol . RestartGraceful ( )
refreshStart ( )
} ( )
time . Sleep ( time . Second )
}
} ( )
mWarnOrig . Hide ( )
refreshMenu := func ( ) {
ok , err := checki2p . CheckI2PIsRunning ( )
if err != nil {
//mWarnOrig.Show()
}
if ok {
mStopOrig . Show ( )
mRestartOrig . Show ( )
mBrowseOrig . Show ( )
} else {
mStopOrig . Hide ( )
mRestartOrig . Hide ( )
mBrowseOrig . Hide ( )
}
i2pcontrol . Initialize ( * host , * port , * path )
_ , err = i2pcontrol . Authenticate ( * password )
if err != nil {
mWarnOrig . Show ( )
mStopOrig . Hide ( )
mRestartOrig . Hide ( )
}
ok , err = checki2pcontrol . CheckI2PControlEcho ( * host , * port , * path , "Will it blend?" )
if err != nil {
mWarnOrig . Show ( )
mStopOrig . Hide ( )
mRestartOrig . Hide ( )
}
if ok {
mWarnOrig . Hide ( )
} else {
mWarnOrig . Show ( )
mStopOrig . Hide ( )
mRestartOrig . Hide ( )
}
}
refreshMenu ( )
go func ( ) {
for {
refreshMenu ( )
log . Println ( "i2pcontrol check succeeded, sleeping for a while" )
time . Sleep ( time . Minute )
}
} ( )
}