Example #1
0
// Init initializes the server.
func Init() error {
	lg.SetSrcHighlight("alkasir/cmd", "alkasir/pkg")
	lg.CopyStandardLogTo("INFO")
	lg.V(1).Info("Log v-level:", lg.Verbosity())
	lg.V(1).Info("Active country codes:", shared.CountryCodes)
	lg.Flush()

	if *datadirFlag == "" {
		u, err := user.Current()
		if err != nil {
			lg.Fatal(err)
		}
		datadir = filepath.Join(u.HomeDir, ".alkasir-central")
	} else {
		datadir = *datadirFlag
	}

	validCountryCodes = make(map[string]bool, len(shared.CountryCodes))
	validCountryCodesMu.Lock()
	for _, cc := range shared.CountryCodes {
		validCountryCodes[cc] = true
	}
	validCountryCodesMu.Unlock()

	err := InitDB()
	if err != nil {
		lg.Fatalln(err)
		return err
	}
	redisPool = newRedisPool(*redisServer, *redisPassword)

	internet.SetDataDir(filepath.Join(datadir, "internet"))

	countryFile := filepath.Join(datadir, "internet", "GeoLite2-Country.mmdb")
	if _, err := os.Stat(countryFile); os.IsNotExist(err) {
		// http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
		lg.Fatalf("cannot enable IP2CountryCode lookups, %s is missing", countryFile)
	} else {
		var err error
		mmCountryDB, err = maxminddb.Open(countryFile)
		if err != nil {
			lg.Fatal(err)
		}
	}

	cityFile := filepath.Join(datadir, "internet", "GeoLite2-City.mmdb")
	if _, err := os.Stat(cityFile); os.IsNotExist(err) {
		// http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
		lg.Warningf("cannot enable IP2CityGeoNameID lookups, %s is missing", cityFile)
	} else {
		mmCityDB, err = maxminddb.Open(cityFile)
		if err != nil {
			lg.Fatal(err)
		}
		// defer mmCityDB.Close()
	}

	return nil
}
Example #2
0
// Init does precondition check if the application can/should be started.
// Init will return an error message with reason for exit printed.
func Run() {
	if debugEnabled {
		log.Println("ALKASIR_DEBUG ENABLED!")
		err := os.Setenv("ALKASIR_DEBUG", "1")
		if err != nil {
			log.Fatal(err)
		}
	}
	if hotEnabled {
		log.Println("ALKASIR_HOT ENABLED!")
		err := os.Setenv("ALKASIR_HOT", "1")
		if err != nil {
			log.Fatal(err)
		}
	}

	// the darwin systray does not exit the main loop
	if runtime.GOOS != "darwin" {
		uiRunning.Add(1)
	}

	err := ui.Run(func() {
		Atexit(ui.Done)

		// start the getpublic ip updater.
		go func() {
			_ = shared.GetPublicIPAddr()
		}()

		if debugEnabled {
			go func() {
				err := http.ListenAndServe(
					fmt.Sprintf("localhost:%d", debugPort), nil)
				if err != nil {
					panic(err)
				}
			}()
		}

		// wipe user data
		if wipeData {
			settingsdir := clientconfig.ConfigPath()
			if settingsdir == "" {
				log.Println("[wipe] Configdir not set")
				os.Exit(1)
			}
			settingsfile := clientconfig.ConfigPath("settings.json")
			if _, err := os.Stat(settingsfile); os.IsNotExist(err) {
				log.Println("[wipe] No settings.json in configdir, will NOT wipe data")
				os.Exit(1)
			}
			log.Println("Wiping all user data")
			if err := os.RemoveAll(settingsdir); err != nil {
				log.Println(err)
			}
		}

		// Prepare logging
		logdir := clientconfig.ConfigPath("log")
		err := os.MkdirAll(logdir, 0775)
		if err != nil {
			log.Println("Could not create logging directory")
			os.Exit(1)
		}
		err = flag.Set("log_dir", logdir)
		if err != nil {
			panic(err)
		}
		lg.SetSrcHighlight("alkasir/cmd", "alkasir/pkg")
		lg.CopyStandardLogTo("INFO")

		// Start init
		if VERSION != "" {
			lg.Infoln("Alkasir v" + VERSION)
		} else {
			lg.Warningln("Alkasir dev version (VERSION not set)")
		}

		lg.V(1).Info("Log v-level:", lg.Verbosity())
		_, err = clientconfig.Read()
		if err != nil {
			lg.Infoln("Could not read config")
			exit()
		}
		lg.V(30).Infoln("settings", clientconfig.Get().Settings)

		if saveChromeExt {
			err := saveChromeExtension()
			if err != nil {
				lg.Fatal(err)
			}
		}

		{
			configChanged, err := clientconfig.UpgradeConfig()
			if err != nil {
				lg.Fatalln("Could not upgrade config", err)

			}
			clientconfig.Update(func(conf *clientconfig.Config) error {

				if clientAuthKeyFlag != "" {
					lg.Warningln("Overriding generated authKey with", clientAuthKeyFlag)
					conf.Settings.Local.ClientAuthKey = clientAuthKeyFlag
					configChanged = true
				}

				if bindAddrFlag != "" {
					lg.Warningln("Overriding configured bindAddr with", bindAddrFlag)
					conf.Settings.Local.ClientBindAddr = bindAddrFlag
					configChanged = true
				}

				if centralAddrFlag != "" {
					lg.Warningln("Overriding central server addr with", centralAddrFlag)
					conf.Settings.Local.CentralAddr = centralAddrFlag
					configChanged = true
				}

				return nil
			})

			if configChanged {
				if err := clientconfig.Write(); err != nil {
					lg.Warning(err)
				}
			}

		}
		conf := clientconfig.Get()
		loadTranslations(LanguageOptions...)
		if err := ui.Language(conf.Settings.Local.Language); err != nil {
			lg.Warningln(err)
		}

		go func() {
			select {
			case <-sigIntC:
				exit()
			case <-ui.Actions.Quit:
				exit()
			}
		}()

		for _, e := range []error{
			mime.AddExtensionType(".json", "application/json"),
			mime.AddExtensionType(".js", "application/javascript"),
			mime.AddExtensionType(".css", "text/css"),
			mime.AddExtensionType(".md", "text/plain"),
		} {
			if e != nil {
				lg.Warning(e)
			}
		}

		err = startInternalHTTPServer(conf.Settings.Local.ClientAuthKey)
		if err != nil {
			lg.Fatal("could not start internal http services")
		}

		// Connect the default transport
		service.UpdateConnections(conf.Settings.Connections)
		service.UpdateTransports(conf.Settings.Transports)
		go service.StartConnectionManager(conf.Settings.Local.ClientAuthKey)

		// TODO: async
		pac.UpdateDirectList(conf.DirectHosts.Hosts)
		pac.UpdateBlockedList(conf.BlockedHostsCentral.Hosts, conf.BlockedHosts.Hosts)
		lastBlocklistChange = time.Now()

		go StartBlocklistUpgrader()
		if upgradeDiffsBaseURL != "" {
			lg.V(19).Infoln("upgradeDiffsBaseURL is ", upgradeDiffsBaseURL)
			go StartBinaryUpgradeChecker(upgradeDiffsBaseURL)
		} else {
			lg.Warningln("empty upgradeDiffsBaseURL, disabling upgrade checks")
		}

		lg.V(5).Info("Alkasir has started")

	})
	// the darwin systray does not exit the main loop
	if runtime.GOOS != "darwin" {
		uiRunning.Done()
	}

	lg.Infoln("ui.Run ended")
	if err != nil {
		log.Println("client.Run error:", err)
	}
}
Example #3
0
func main() {

	lg.SetSrcHighlight("alkasir/cmd", "alkasir/pkg")
	lg.CopyStandardLogTo("INFO")
	lg.V(1).Info("Log v-level:", lg.Verbosity())
	lg.V(1).Info("Active country codes:", shared.CountryCodes)
	lg.Flush()

	if obfs4proxy.Check() {
		monitorBindAddr := flag.String("monitorAddr", "localhost:8037", "port to bind monitor server to")
		flag.Parse()
		go startMonitoring(*monitorBindAddr)

		obfs4proxy.Run(obfs4proxy.Config{
			LogLevel:      "INFO",
			EnableLogging: true,
			UnsafeLogging: false,
		})
		os.Exit(0)
	}

	monitorBindAddr := flag.String("monitorAddr", "localhost:8036", "port to bind monitor server to")
	flag.Parse()
	go startMonitoring(*monitorBindAddr)

	// server := ptc.Server{
	// 	Config: ptc.Config{
	// 		StateLocation: ".",
	// 		Command:       os.Args[0],
	// 	},
	// 	ORPort: "127.0.0.1:23450",
	// 	Methods: []ptc.SMethod{ptc.SMethod{
	// 		MethodName: "obfs4",
	// 		BindAddr:   "127.0.0.1:4423",
	// 		Args: ptc.Args{
	// 			"node-id":     []string{"4d3e4561149907025571827a1277661b5f9fca46"},
	// 			"private-key": []string{"7886017adfd178cd139e91250dddee0b2af8ab6cf93f1fe9a7a469d3a13a3067"},
	// 			"public-key":  []string{"4393d7641042620a60881a72ebb47ef8c5a5840acb4f858e5c0026cb2e75fd6f"},
	// 			"drbg-seed":   []string{"c23e876ddc408cc392317e017a6796a96161f76d8dd90522"},
	// 			"iat-mode":    []string{"0"},
	// 		},
	// 	},
	// 	},
	// }

	configdir := os.Getenv("ALKASIR_TORPT_CONFIGDIR")
	configfile := filepath.Join(configdir, "alkasir-torpt-server.json")

	data, err := ioutil.ReadFile(configfile)
	if err != nil {
		panic(err)
	}

	server := ptc.Server{}
	err = json.Unmarshal(data, &server)
	if err != nil {
		panic(err)
	}
	server.Command = os.Args[0]

	go socks5server(server.ORPort)

	err = server.Start()
	if err != nil {
		panic(err)
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		err := server.Wait()
		if err != nil {
			log.Println(err)
		}
	}()
	wg.Wait()

}