Files
checki2cp/checki2cp.go

256 lines
7.1 KiB
Go
Raw Normal View History

2019-07-10 22:46:58 -04:00
package checki2p
import (
"fmt"
2020-01-21 21:33:10 -05:00
"io/ioutil"
2019-07-10 22:46:58 -04:00
"log"
"os"
2020-04-03 17:30:48 -04:00
"os/exec"
"os/user"
2020-01-21 21:33:10 -05:00
"runtime"
"strings"
2020-05-14 21:48:26 -04:00
"github.com/eyedeekay/go-i2cp"
2019-07-10 22:46:58 -04:00
)
func inithome(str string) string {
s, e := os.UserHomeDir()
if e != nil {
panic(e)
}
return s + str
}
func checkfileexists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else if os.IsNotExist(err) {
return false
}
return false
}
2020-01-21 21:33:10 -05:00
func home() string {
if runtime.GOOS == "windows" {
return "\\i2p"
}
return "/.i2p"
}
2019-07-10 22:46:58 -04:00
var (
2020-05-15 01:37:34 -04:00
// I2CP_HOST is the i2cp host
I2CP_HOST string = ""
// I2CP_PORT is the i2cp port
I2CP_PORT string = ""
// WINDOWS_DEFAULT_LOCATION
WINDOWS_DEFAULT_LOCATION string = `C:\\Program Files\i2p\i2psvc.exe`
// I2PD_WINDOWS_DEFAULT_LOCATION
I2PD_WINDOWS_DEFAULT_LOCATION string = `C:\\Program Files\I2Pd\i2pd.exe`
// LINUX_SYSTEM_LOCATION
LINUX_SYSTEM_LOCATION []string = []string{"/usr/bin/i2prouter", "/usr/sbin/i2prouter"}
// LINUX_I2PD_SYSTEM_LOCATION
I2PD_LINUX_SYSTEM_LOCATION string = "/usr/sbin/i2pd"
// I2P_ASUSER_HOME_LOCATION This is the path to the default I2P config directory when running as a user
I2P_ASUSER_HOME_LOCATION string = inithome(home())
// HOME_DIRECTORY_LOCATION can use config/run from a user's $HOME directory, this is the path to that router
HOME_DIRECTORY_LOCATION string = inithome("/i2p/i2prouter")
// OSX_DEFAULT_LOCATION
OSX_DEFAULT_LOCATION string = inithome("/Library/Application Support/i2p/clients.config")
2019-07-10 22:46:58 -04:00
)
2020-05-15 17:49:28 -04:00
func i2pdArgs() ([]string, error) {
return []string{""}, nil
}
2019-07-10 22:46:58 -04:00
// CheckIC2PIsRunning is frequently the only thing I need a reliable, non-SAM
// way to test at runtime.
func CheckI2PIsRunning() (bool, error) {
client := go_i2cp.NewClient(nil)
err := client.Connect()
if err != nil {
return false, err
}
destination, err := go_i2cp.NewDestination()
if err != nil {
return false, err
}
if destination == nil {
return false, fmt.Errorf("")
}
client.Disconnect()
return true, nil
}
2019-07-10 22:49:38 -04:00
// CheckI2PIsInstalledDefaultLocation looks in various locations for the
// presence of an I2P router.
2019-07-10 22:46:58 -04:00
func CheckI2PIsInstalledDefaultLocation() (bool, error) {
if checkfileexists(I2PD_WINDOWS_DEFAULT_LOCATION) {
log.Println("Windows i2pd router detected")
return true, nil
}
if checkfileexists(I2PD_LINUX_SYSTEM_LOCATION) {
log.Println("Linux i2pd router detected")
return true, nil
}
if checkfileexists(WINDOWS_DEFAULT_LOCATION) {
log.Println("Windows i2p router detected")
return true, nil
}
2020-04-03 17:05:54 -04:00
if checkfileexists(LINUX_SYSTEM_LOCATION[0]) {
log.Println("Linux i2p router detected")
return true, nil
}
if checkfileexists(LINUX_SYSTEM_LOCATION[1]) {
2019-07-10 22:46:58 -04:00
log.Println("Linux i2p router detected")
return true, nil
}
if checkfileexists(HOME_DIRECTORY_LOCATION) {
log.Println("Linux i2p router detected")
return true, nil
}
if checkfileexists(OSX_DEFAULT_LOCATION) {
log.Println("OSX i2p router detected")
return true, nil
}
return false, nil
}
2020-04-10 23:17:34 -04:00
// UserFind makes sure that we never mis-identify the user account because of
// sudo
func UserFind() string {
if os.Geteuid() == 0 {
str := os.Getenv("SUDO_USER")
return str
}
if un, err := user.Current(); err == nil {
return un.Name
}
return ""
}
// CheckI2PUserName looks in various locations for the
// presence of an I2P router and guesses the username it
// should run under. On Windows it returns the EXE name.
func CheckI2PUserName() (string, error) {
if checkfileexists(I2PD_WINDOWS_DEFAULT_LOCATION) {
log.Println("Windows i2pd router detected")
2019-10-26 23:13:06 -04:00
return "i2pd", nil
}
if checkfileexists(I2PD_LINUX_SYSTEM_LOCATION) {
log.Println("Linux i2pd router detected")
return "i2pd", nil
}
if checkfileexists(WINDOWS_DEFAULT_LOCATION) {
log.Println("Windows i2p router detected")
2019-10-26 23:13:06 -04:00
return "i2psvc", nil
}
2020-04-03 17:05:54 -04:00
if checkfileexists(LINUX_SYSTEM_LOCATION[0]) {
log.Println("Linux i2p router detected")
return "i2psvc", nil
}
if checkfileexists(LINUX_SYSTEM_LOCATION[1]) {
log.Println("Linux i2p router detected")
return "i2psvc", nil
}
if checkfileexists(HOME_DIRECTORY_LOCATION) {
log.Println("Linux i2p router detected")
return UserFind(), nil
}
if checkfileexists(OSX_DEFAULT_LOCATION) {
log.Println("OSX i2p router detected")
return UserFind(), nil
}
return "", nil
}
2020-01-21 21:33:10 -05:00
// GetFirewallPort finds the configured UDP port of your I2P router to help
// configure firewalls. It does this by finding the router.config and reading
2020-05-15 01:29:02 -04:00
// it. This function will not work on I2Pd routers yet but it should be easy
// to add support once I get some more time to test and research it.
2020-01-21 21:33:10 -05:00
func GetFirewallPort() (string, error) {
log.Println(I2P_ASUSER_HOME_LOCATION)
file, err := ioutil.ReadFile(I2P_ASUSER_HOME_LOCATION + "/router.config")
if err != nil {
return "", err
}
lines := strings.Split(string(file), "\n")
for index, line := range lines {
if strings.HasPrefix(line, "i2np.udp.port") {
log.Println(line, index)
return strings.Replace(line, "i2np.udp.port=", "", -1), nil
}
}
return "", fmt.Errorf("Improperly formed router.config file")
}
2020-04-03 17:05:54 -04:00
// FindI2PIsInstalledDefaultLocation looks in various locations for the
// presence of an I2P router, reporting back the location
func FindI2PIsInstalledDefaultLocation() (string, error) {
if checkfileexists(I2PD_WINDOWS_DEFAULT_LOCATION) {
log.Println("Windows i2pd router detected")
return I2PD_WINDOWS_DEFAULT_LOCATION, nil
}
if checkfileexists(I2PD_LINUX_SYSTEM_LOCATION) {
log.Println("Linux i2pd router detected")
return I2PD_LINUX_SYSTEM_LOCATION, nil
}
if checkfileexists(WINDOWS_DEFAULT_LOCATION) {
log.Println("Windows i2p router detected")
return WINDOWS_DEFAULT_LOCATION, nil
}
if checkfileexists(LINUX_SYSTEM_LOCATION[0]) {
log.Println("Linux i2p router detected")
return LINUX_SYSTEM_LOCATION[0], nil
}
if checkfileexists(LINUX_SYSTEM_LOCATION[1]) {
log.Println("Linux i2p router detected")
return LINUX_SYSTEM_LOCATION[1], nil
}
if checkfileexists(HOME_DIRECTORY_LOCATION) {
log.Println("Linux i2p router detected")
return HOME_DIRECTORY_LOCATION, nil
}
if checkfileexists(OSX_DEFAULT_LOCATION) {
log.Println("OSX i2p router detected")
return OSX_DEFAULT_LOCATION, nil
}
return "", fmt.Errorf("i2p router not found.")
}
2020-04-03 17:30:48 -04:00
2020-04-10 23:17:34 -04:00
// ConditionallyLaunchI2P If an already-installed I2P router is present, then
// make sure that it is started, i.e. launch the router *only* if it is not
// already running.
2020-04-03 17:30:48 -04:00
func ConditionallyLaunchI2P() (bool, error) {
ok, err := CheckI2PIsInstalledDefaultLocation()
if err != nil {
return false, err
}
if ok {
if ok, err := CheckI2PIsRunning(); err == nil {
if !ok {
path, err := FindI2PIsInstalledDefaultLocation()
if err != nil {
return false, err
}
if strings.HasSuffix(path, "i2prouter") {
cmd := exec.Command(path, "start")
if err := cmd.Start(); err != nil {
2020-04-10 23:17:34 -04:00
return false, fmt.Errorf("I2P router startup failure %s", err)
}
} else {
cmd := exec.Command(path, "--daemon")
if err := cmd.Start(); err != nil {
2020-04-10 23:17:34 -04:00
return false, fmt.Errorf("I2P router startup failure %s", err)
}
}
return true, nil
}
return true, nil
} else {
return false, err
}
2020-04-03 17:30:48 -04:00
} else {
return false, fmt.Errorf("I2P is not a default location, please set $I2P environment variable")
}
}