Esempio n. 1
0
func Initialize(newAddressList *addresslist.SafeIPList,
	newAddressSet *addresslist.AddressSet,
	newBlackWhiteList *addresslist.BlackWhiteList,
	newFileIndex *fileindex.SafeFileList,
	newLocalIP net.IP) {
	addressList = newAddressList
	addressSet = newAddressSet
	blackWhiteList = newBlackWhiteList
	fileIndex = newFileIndex
	localIP = newLocalIP
	mux := http.NewServeMux()

	mux.HandleFunc("/peerlist", makeBWListHandler(makeGzipHandler(peerListHandler)))
	mux.HandleFunc("/ping", makeBWListHandler(pingHandler))
	mux.HandleFunc("/fileindex", makeBWListHandler(indexForHandler))
	mux.HandleFunc("/version", versionHandler)
	mux.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir(util.SandwichPath))).(http.HandlerFunc))

	log.Printf("About to listen on %s.\n", util.GetPort(localIP))
	srv := &http.Server{Handler: mux, Addr: util.GetPort(localIP)}
	err := srv.ListenAndServe()
	if err != nil {
		log.Fatal(err)
	}
}
Esempio n. 2
0
func writePeers() {
	peerList := make([]IPPort, 0, 10)
	for _, peer := range addressList.Contents() {
		peerList = append(peerList, IPPort{peer.IP.String(), util.GetPort(peer.IP)})
	}
	json_res, _ := json.Marshal(peerList)
	peerHub.broadcast <- string(json_res)
}
Esempio n. 3
0
func DownloadFile(address net.IP, filePath string) error {
	if !blackWhiteList.OK(address) {
		return illegalIPError
	}
	log.Println("Beginning download of " + filePath)
	conn, err := net.DialTimeout("tcp", address.String()+util.GetPort(address),
		2*time.Minute)
	if err != nil {
		return err
	}
	defer conn.Close()

	url := url.URL{}
	url.Path = filePath
	request, err := http.NewRequest("GET", "/files/"+url.String(), nil)
	if err != nil {
		return err
	}

	err = request.Write(conn)
	if err != nil {
		return err
	}
	buffer := bufio.NewReader(conn)
	response, err := http.ReadResponse(buffer, request)
	if err != nil {
		return err
	}
	buffer = bufio.NewReader(response.Body)
	dirPath, _ := filepath.Split(filePath)
	err = os.MkdirAll(path.Join(util.SandwichPath, dirPath), os.ModePerm)
	if err != nil {
		return err
	}
	file, err := os.Create(path.Join(util.SandwichPath, filePath))
	if err != nil {
		file.Close()
		return err
	}
	byteBuf := make([]byte, 4*1024*1024)
	for done := false; !done; {
		numRead, err := buffer.Read(byteBuf)
		if err == io.EOF {
			done = true
		} else if err != nil {
			file.Close()
			return err
		}
		_, err = file.Write(byteBuf[:numRead])
		if err != nil {
			file.Close()
			return err
		}
	}
	err = file.Close()
	log.Println("File download complete: " + path.Join(util.SandwichPath, filePath))
	return err
}
Esempio n. 4
0
func ManifestMap() IPFilePairs {
	fileManifest = client.CleanManifest(fileManifest)
	log.Println("FileManifest was updated.")
	fileList := make(IPFilePairs, 0, 100)
	for ipString, tempFileList := range fileManifest {
		ip := net.ParseIP(ipString)
		port := util.GetPort(ip)
		for _, fileItem := range tempFileList.List {
			fileList = append(fileList, &IPFilePair{NetIP(ip), port, fileItem.FileName})
		}
	}
	return fileList
}
func get(address net.IP, extension string) ([]byte, error) {
	if !blackWhiteList.OK(address) {
		return nil, illegalIPError
	}
	conn, err := net.DialTimeout("tcp", address.String()+util.GetPort(address),
		2*time.Second)
	if err != nil {
		return nil, err
	}
	defer conn.Close()
	request, err := http.NewRequest("GET", extension, nil)
	if err != nil {
		return nil, err
	}
	request.Header = map[string][]string{
		"Accept-Encoding": {"gzip"},
	}
	err = request.Write(conn)
	if err != nil {
		return nil, err
	}
	buffer := bufio.NewReader(conn)
	response, err := http.ReadResponse(buffer, request)
	if err != nil {
		return nil, err
	}
	var data []byte
	if strings.Contains(response.Header.Get("Content-Encoding"), "gzip") {
		unzipper, err := gzip.NewReader(response.Body)
		if err != nil {
			return nil, err
		}
		data, err = ioutil.ReadAll(unzipper)
	} else {
		data, err = ioutil.ReadAll(response.Body)
	}
	if err != nil {
		return nil, err
	}
	return data, err
}