Files
checki2cp/i2pdbundle/launch_linux.go

106 lines
2.5 KiB
Go
Raw Permalink Normal View History

package i2pd
2020-05-14 18:04:53 -04:00
import (
2020-05-14 21:48:26 -04:00
"fmt"
2020-05-14 18:04:53 -04:00
"log"
"os"
2020-05-14 21:48:26 -04:00
"os/exec"
2020-05-14 18:04:53 -04:00
"path/filepath"
2020-05-14 21:48:26 -04:00
"github.com/eyedeekay/checki2cp"
"github.com/eyedeekay/checki2cp/proxycheck"
"github.com/eyedeekay/checki2cp/samcheck"
2020-05-14 18:04:53 -04:00
)
2020-05-15 19:32:12 -04:00
// FindI2Pd returns the absolute path to the i2pd executable
2020-05-14 18:04:53 -04:00
func FindI2Pd() (string, error) {
path, err := UnpackI2PdPath()
if err != nil {
return "", err
}
if err := FileOK(filepath.Join(path, "i2pd")); err != nil {
return "", err
}
return filepath.Join(path, "i2pd"), nil
}
2020-05-14 21:48:26 -04:00
// UnpackI2Pd unpacks a working version of i2pd and some supporting libraries to a the directory of the executable
// that will start i2pd
2020-05-14 18:04:53 -04:00
func UnpackI2Pd() error {
dir, err := UnpackI2PdPath()
if err != nil {
return err
}
err = WriteAllFiles(dir)
if err != nil {
return err
}
return nil
}
2020-05-15 19:32:12 -04:00
// LaunchI2PdConditional will check for specific services and if they are not found, start a standalone router
2020-05-15 13:15:56 -04:00
func LaunchI2PdConditional(needI2CP, needProxy, needSAM bool) (*exec.Cmd, error) {
if needI2CP {
if notRunning, inError := checki2p.CheckI2PIsRunning(); inError != nil {
return nil, inError
} else if notRunning {
return nil, fmt.Errorf("I2P is already running with an open I2CP port")
}
2020-05-14 18:04:53 -04:00
}
2020-05-15 13:15:56 -04:00
if needProxy {
if checkproxy.ProxyDotI2P() {
return nil, fmt.Errorf("I2P is already running with an open HTTP proxy")
}
}
2020-05-15 13:15:56 -04:00
if needSAM {
if checksam.CheckSAMAvailable("") {
return nil, fmt.Errorf("I2P is already running with an open SAM API")
}
}
2020-05-15 13:15:56 -04:00
return LaunchI2PdForce()
}
// LaunchI2Pd will look for a running I2P router and if one is not found, it will start the embedded I2P router
func LaunchI2Pd() (*exec.Cmd, error) {
return LaunchI2PdConditional(true, true, true)
}
2020-05-15 19:32:12 -04:00
// LaunchI2PdForce attempts to launch the embedded I2P router no matter what.
2020-05-15 13:15:56 -04:00
func LaunchI2PdForce() (*exec.Cmd, error) {
libPath, err := UnpackI2PdLibPath()
if err != nil {
return nil, err
}
if err := FileOK(libPath); err != nil {
return nil, err
}
i2pd, err := FindI2Pd()
2020-05-15 18:59:58 -04:00
if err := FileOK(i2pd); err != nil {
return nil, err
}
err = os.Setenv("LD_LIBRARY_PATH", libPath)
if err != nil {
return nil, err
}
log.Println(i2pd)
runDir, err := UnpackI2PdDir()
if err != nil {
return nil, err
}
cmd := exec.Command(
i2pd,
"--datadir="+runDir,
"--conf="+filepath.Join(runDir, "i2pd.conf"),
"--tunconf="+filepath.Join(runDir, "tunnels.conf"),
"--log=none",
)
cmd.Env = append(os.Environ(),
"LD_LIBRARY_PATH="+libPath, // ignored
)
log.Printf("running command: %v %s", cmd.Env, cmd.String())
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
return cmd, nil
2020-05-14 18:04:53 -04:00
}