// CreateMeta - This function creates a new metainfo file for use within the GUI server // @param name string - The name of the new lynk func CreateMeta(name string) error { tDir, err := os.Stat(lynxutil.HomePath + name) // Checks to see if the directory exists if err != nil || !tDir.IsDir() { fmt.Println("ERROR!") return errors.New("Directory " + name + "does not exist in the Lynx directory.") } metaFile, err := os.Create(lynxutil.HomePath + name + "/meta.info") if err != nil { fmt.Println(err) return err } currentUser, _ := user.Current() metaFile.WriteString("announce:::" + lynxutil.GetIP() + ":" + lynxutil.TrackerPort + "\n") metaFile.WriteString("lynkName:::" + name + "\n") metaFile.WriteString("owner:::" + currentUser.Name + "\n") addLynk(name, currentUser.Name) filepath.Walk(lynxutil.HomePath+name, visitFiles) parseMetainfo(lynxutil.HomePath + name + "/meta.info") return nil // Everything was fine if we reached this point }
// CreateSwarm - Creates a new swarm.info upon clicking of create button in gui // @param string name - the name of the lynk func CreateSwarm(name string) { p1 := lynxutil.Peer{IP: "", Port: "8080"} currentuser, err := user.Current() trackerDir := currentuser.HomeDir + "/Lynx/" + name + "/" + name + "_Tracker" os.Mkdir(trackerDir, 0755) _, err = os.Create(trackerDir + "/swarm.info") if err != nil { fmt.Println(err) } p1.IP = lynxutil.GetIP() addToSwarminfo(p1, trackerDir+"/swarm.info") lynxutil.FileCopy(currentuser.HomeDir+"/Lynx/"+name+"/meta.info", trackerDir+"/meta.info") }
// DeleteLynk - This function deletes a Lynk based upon its name from the list of lynks // @param nameToDelete string - the lynk we want to remove func DeleteLynk(nameToDelete string, deleteLocal bool) { i := 0 for i < len(lynks) { if nameToDelete == lynks[i].Name { // Removes this peer from swarm.info file conn, _ := net.Dial("tcp", lynks[i].Tracker) fmt.Fprintf(conn, "Disconnect:"+lynxutil.GetIP()+":"+lynks[i].Name+"\n") lynks = append(lynks[:i], lynks[i+1:]...) } i++ } updateLynksFile() if deleteLocal { os.RemoveAll(lynxutil.HomePath + nameToDelete) } }
// Asks the tracker for a list of peers and then places them into a lynk's peers array // @param string lynkName - The name of the lynk we're interested in func askTrackerForPeers(lynkName string) error { lynk := lynxutil.GetLynk(lynks, lynkName) // Connects to tracker conn, err := net.Dial("tcp", lynk.Tracker) if err != nil { i := 0 for i < len(lynk.Peers) && err != nil { pConn, _ := net.Dial("tcp", lynk.Peers[i].IP+":"+lynk.Peers[i].Port) fmt.Fprintf(pConn, "Tracker_Request:"+lynkName+"/\n") reply := "" reply, err = bufio.NewReader(pConn).ReadString('\n') // Waits for a String ending in newline reply = strings.TrimSpace(reply) conn, err = net.Dial("tcp", reply) i++ } // We could not connect to the tracker if err != nil { return err } } // Gives IP and ServerPort So It Can Be Added To swarm.info fmt.Fprintf(conn, "Swarm_Request:"+lynxutil.GetIP()+":"+lynxutil.ServerPort+":"+lynkName+"\n") reader := bufio.NewReader(conn) tp := textproto.NewReader(reader) reply, err := tp.ReadLine() //fmt.Println(reply) // Tracker will close connection when finished - which will break us out of this loop for err == nil { peerArray := strings.Split(reply, ":::") tmpPeer := lynxutil.Peer{IP: peerArray[0], Port: peerArray[1]} if !contains(lynk.Peers, tmpPeer) { lynk.Peers = append(lynk.Peers, tmpPeer) } reply, err = tp.ReadLine() } return nil // Did not have an error if we reached this point }