Example #1
0
func setupDatabaseTryConnect(in *bufio.Reader) error {
	checkError(connectDB())
	_, err := db.Exec("SELECT datname FROM pg_database WHERE datname = $1 LIMIT 1", config.DBName)
	if err != nil {
		fmt.Printf("\nThere was an error connecting to the database: %v\n\n", err)
		fmt.Println("To see the SQL needed to create the database and user, type 'sql'.\n")
		fmt.Print("Reconfigure the database settings [yes]? ")
		line, oerr := in.ReadString('\n')
		checkError(oerr)
		ans := strings.TrimSpace(line)
		fmt.Println("")
		if strings.HasPrefix("yes", ans) {
			setupDatabase(in)
		} else if ans == "sql" {
			if config.DBPass == "" {
				config.DBPass = common.RandomString(32)
			}

			sql := `CREATE USER ` + common.EscapeSQL(config.DBUser) + `;
ALTER USER ` + common.EscapeSQL(config.DBUser) + ` WITH PASSWORD '` + common.EscapeSQL(config.DBPass) + `';
CREATE DATABASE ` + common.EscapeSQL(config.DBName) + `
  WITH OWNER      = ` + common.EscapeSQL(config.DBUser) + `
       ENCODING   = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'en_US.UTF-8'
       LC_CTYPE   = 'en_US.UTF-8'
       CONNECTION LIMIT = -1;
GRANT ALL PRIVILEGES ON DATABASE ` + common.EscapeSQL(config.DBName) + ` TO ` + common.EscapeSQL(config.DBUser) + `;`
			fmt.Println("-- SQL statement")
			fmt.Println("--------------------------------------")
			fmt.Println(sql)
			fmt.Println("--------------------------------------")
			fmt.Println("")
			fmt.Println("Run 'sudo -u postgres psql postgres' and paste the above SQL to create the user and database.")
			fmt.Println("")
			fmt.Print("Try to connect again [yes]? ")
			line, oerr := in.ReadString('\n')
			checkError(oerr)
			ans := strings.TrimSpace(line)
			if strings.HasPrefix("yes", ans) {
				setupDatabaseTryConnect(in)
				return nil
			}
		}
		return err
	}
	fmt.Println("")
	fmt.Println("Database connection established.")
	return nil
}
Example #2
0
// Connects to a URL (preferably external) to determine if we reach ourselves, i.e. this
// machine is its own proxy server, and returns true if it is. If there is an error running
// a dummy server on the designated port, that is returned.
func CheckProxyLoop(port uint16, url string) (bool, error) {
	pstr := fmt.Sprintf("127.0.0.1:%d", port)
	testRandom := common.RandomString(32)
	testHandler := &EnvProxyTest{
		Random: testRandom,
	}
	testSrv := &http.Server{
		Addr:    pstr,
		Handler: testHandler,
	}
	l, err := net.Listen("tcp", pstr)
	if err != nil {
		return false, fmt.Errorf("Couldn't listen on %s for EnvProxyTest", pstr)
	}
	defer l.Close() // this kills testSrv
	go testSrv.Serve(l)
	http.DefaultClient.Get(url + "?sniffyEnvProxyTestRandom=" + testRandom)
	if testHandler.Loop {
		return true, nil
	}
	return false, nil
}