示例#1
0
func startServer() {
	log.Println("starting server")
	var redisErr error
	r, redisErr = redis.NewClient(*redisHost, *redisDB)
	util.CheckError(redisErr, shutdown)

	sExists, sErr := r.KeyExists("secret")
	util.CheckError(sErr, shutdown)

	if !sExists {
		log.Println("running server for the first time")
		setErr := r.GenerateGlobalSecret()
		util.CheckError(setErr, shutdown)
	}

	getErr := r.RetrieveGlobalSecret()
	util.CheckError(getErr, shutdown)

	var lErr error
	l, lErr = tcp.NewListener(*tcpPort, nil)
	util.CheckError(lErr, shutdown)

	srv = server.New("/register", "/validate", "/file", *httpPort, r, l)
	startErr := srv.Start()
	util.CheckError(startErr, shutdown)

	l.Start()
}
示例#2
0
func requestFile(shareHash string) (err error) {
	resp, respErr := http.Get("http://" + *httpServerAddress + "/file?action=request&sh=" + shareHash)
	util.CheckError(respErr, shutdown)
	body, bodyErr := ioutil.ReadAll(resp.Body)
	util.CheckError(bodyErr, shutdown)
	resp.Body.Close()

	bSl := strings.Split(string(body), "::::")

	switch bSl[0] {
	case "NOFW":
		log.Println("pulling directly from client")
		fResp, respErr := http.Get("http://" + bSl[1] + "/provide?sh=" + shareHash)
		util.CheckError(respErr, shutdown)
		fBody, fBodyErr := ioutil.ReadAll(fResp.Body)
		util.CheckError(fBodyErr, shutdown)
		fResp.Body.Close()

		bodyStr := string(fBody)
		sep := "::"
		fileName := strings.Split(bodyStr, sep)[0]
		fileContents := bodyStr[len(fileName)+len(sep):]

		saveErr := util.SaveFile(fileName, []byte(fileContents))
		util.CheckError(saveErr, shutdown)

		log.Printf("file %s saved\n", fileName)
	case "FW":
		fileName := bSl[1]
		fileContents := []byte(bSl[2])
		log.Println("pulling through server")

		saveErr := util.SaveFile(fileName, fileContents)
		util.CheckError(saveErr, shutdown)

		log.Printf("file %s saved\n", fileName)
	}

	return
}
示例#3
0
func main() {
	if *serverMode {
		startServer()
	} else if *showShared {
		md = meta.NewData()
		err := readConfig()
		if err != nil {
			util.CheckError(err, shutdown)
		}

		for k, v := range md.Files {
			fmt.Println(md.UserID+k, v.Path)
		}

		shutdown()
	} else {
		md = meta.NewData()
		if *removeHash != "" {
			rmErr := removeSharedFile(*removeHash)
			util.CheckError(rmErr, shutdown)
			shutdown()
		}
		if len(os.Args) > 1 {
			if *daemonMode {
				startDaemon()
			} else {
				filePath := os.Args[1]

				err := readConfig()
				if err != nil {
					util.CheckError(err, shutdown)
				}

				*tcpServerAddress = md.TCPServer
				*httpServerAddress = md.HTTPServer

				if len(filePath) == 96 && !strings.Contains(filePath, "/") {
					requestFile(filePath)
					shutdown()
				} else {
					fileStat, fileStatErr := os.Stat(filePath)
					util.CheckError(fileStatErr, shutdown)

					if fileStat.IsDir() {
						util.CheckError(errors.New(filePath+" is a directory"), shutdown)
					}

					currentDir, currentDirErr := os.Getwd()
					util.CheckError(currentDirErr, shutdown)

					if string(filePath[0]) != "/" {
						filePath = currentDir + "/" + filePath
					}

					shareErr := shareFile(filePath)
					util.CheckError(shareErr, shutdown)
					shutdown()
				}
			}
		} else {
			util.CheckError(errors.New("please provide required arguments"), shutdown)
		}
	}

	// Shutting down on SIGINT.
	go func() {
		intChan := make(chan os.Signal)
		signal.Notify(intChan, os.Interrupt)

		<-intChan
		log.Println("Shutting down gracefully")
		go shutdown()

		fmt.Println("send SIGINT again to kill")
		<-intChan

		os.Exit(1)
	}()

	select {}
}
示例#4
0
func startDaemon() {
	log.Println("starting daemon")

	configPath := os.Getenv("HOME") + "/.togepi/data"
	configStat, configStatErr := os.Stat(configPath)

	md.ConfPath = configPath

	switch {
	case os.IsNotExist(configStatErr):
		log.Println("first start, generating configuration")

		resp, respErr := http.Get("http://" + *httpServerAddress + "/register")
		util.CheckError(respErr, shutdown)
		body, bodyErr := ioutil.ReadAll(resp.Body)
		util.CheckError(bodyErr, shutdown)
		resp.Body.Close()

		var respStruct server.RegResp
		jsonRespErr := json.Unmarshal(body, &respStruct)
		util.CheckError(jsonRespErr, shutdown)

		md.SetUserData(respStruct.UserID, respStruct.UserKey)
		md.SetServerData(*tcpServerAddress, *httpServerAddress)
		dataErr := md.CreateDataFile(configPath)
		util.CheckError(dataErr, shutdown)
	case configStat.IsDir():
		util.CheckError(errors.New(configPath+" is a directory"), shutdown)
	default:
		readDataErr := md.ReadDataFile(configPath)
		util.CheckError(readDataErr, shutdown)

		*httpServerAddress = md.HTTPServer
		*tcpServerAddress = md.TCPServer

		resp, respErr := http.Get("http://" + *httpServerAddress + "/validate?uid=" + md.UserID + "&ukey=" + md.UserKey)
		util.CheckError(respErr, shutdown)

		if resp.StatusCode != http.StatusOK {
			util.CheckError(errors.New("invalid user"), shutdown)
		}
	}

	var clErr error
	cl, clErr = tcp.NewClient(md, *tcpServerAddress, *socketPort, *providerPort)
	util.CheckError(clErr, shutdown)

	cl.HandleServerCommands()

	var lErr error
	l, lErr = tcp.NewListener(*socketPort, md)
	util.CheckError(lErr, shutdown)

	p = server.NewProvider("/provide", *providerPort, md)
	pErr := p.Start()
	util.CheckError(pErr, shutdown)

	l.AcceptConnections(*httpServerAddress, md.UserID, md.UserKey)
}