Exemple #1
0
// 输出Hash
func OutHash(master chan string) {
	for {
		select {
		case infohash := <-master:
			if models.DbConfig.ShowMsg {
				fmt.Println("Get InfoHash: ", infohash)
			}
			// 定义一个SC_Hash
			var schash models.SC_Hash
			// 设置SC_Hash
			schash.Hot = 1
			schash.IsPut = false
			schash.InfoHash = strings.ToUpper(strings.TrimSpace(infohash))
			// 检测infohash是否存在
			has := models.Has(models.DbHash, bson.M{"infohash": schash.InfoHash})
			// 保存hash数据
			err := schash.Save()
			if err == nil && has == false {
				// 自增统计数据
				models.SaveLog(time.Now().Format("20060102"), "dhtnums")
			}

			// 修改种子热度
			models.SetHot(schash.InfoHash)
		}
	}
}
Exemple #2
0
// 磁力链转种子
func (this *IndexController) Torrent() {
	if this.Ctx.Request.Method == "POST" {
		// 获取磁力链接
		magnet := this.GetString("magnetLink")
		// 将磁力链接转换为大写格式
		magnet = strings.ToUpper(magnet)

		// 定义一个正则
		re, _ := regexp.Compile(`MAGNET:\?XT=URN:BTIH:([^&]+)`)
		// 匹配磁力链接
		match := re.FindAllString(magnet, -1)
		// 获取infohash
		magnet = strings.Replace(match[0], "MAGNET:?XT=URN:BTIH:", "", -1)
		// 去除空格并转换为大写
		magnet = strings.ToUpper(strings.TrimSpace(magnet))

		if match[0] == "" {
			// 如果匹配不到磁力链接
			if len(magnet) != 40 {
				// 跳转404
				this.Abort("404")
			}
		}

		// 检测infohash是否已经存在
		if !models.Has(models.DbHash, bson.M{"infohash": magnet}) {
			// 定义一个SC_Hash
			var schash models.SC_Hash
			// 设置SC_Hash
			schash.Hot = 1
			schash.IsPut = true
			schash.InfoHash = magnet
			// 保存hash数据
			err := schash.Save()
			if err == nil {
				// 自增统计数据
				models.SaveLog(time.Now().Format("20060102"), "dhtnums")
			}
		}

		// 检测infohash是否已入库过
		if !models.Has(models.DbInfo, bson.M{"infohash": magnet}) {
			// 下载并入库种子
			ret, err := common.PullTorrent(magnet)
			if err != nil || ret != 0 {
				this.Abort("404")
			}
		}

		// 跳转到种子信息页
		this.Redirect("/"+magnet, 302)
	}

	this.TplNames = "torrent.html"
}
Exemple #3
0
// 种子转磁力链
func (this *IndexController) Magnet() {
	if this.Ctx.Request.Method == "POST" {
		// 获取上传文件
		f, h, err := this.GetFile("torrentFile")
		if err != nil {
			// 跳转404
			this.Abort("404")
		}
		// 保证正确关闭
		defer f.Close()

		// 获取最后一个.出现的位置
		index := strings.LastIndex(h.Filename, ".")
		// 获取文件后缀
		ext := strings.ToUpper(h.Filename[index+1:])
		// 若不是种子文件后缀则报错
		if ext != "TORRENT" {
			// 跳转404
			this.Abort("404")
		}

		// 读取种子信息
		meta, err := common.ReadTorrent(f)
		if err != nil || len(meta.InfoHash) != 40 {
			// 跳转404
			this.Abort("404")
		}

		// 获取种子UTF-8格式名称
		caption := meta.Info.Name8
		if caption == "" {
			// 获取失败则获取默认名称
			caption = meta.Info.Name
		}

		// 种子入库
		err = common.PutTorrent(meta)
		if err == nil {
			// 检测infohash是否已经存在
			if models.Has(models.DbHash, bson.M{"infohash": strings.ToUpper(strings.TrimSpace(meta.InfoHash))}) {
				// 设置当前infohash已经入库
				models.SetPut(strings.ToUpper(strings.TrimSpace(meta.InfoHash)))
			} else {
				// 定义一个SC_Hash
				var schash models.SC_Hash
				// 设置SC_Hash
				schash.Hot = 1
				schash.IsPut = true
				schash.InfoHash = strings.ToUpper(strings.TrimSpace(meta.InfoHash))
				// 保存hash数据
				err = schash.Save()
				if err == nil {
					// 自增统计数据
					models.SaveLog(time.Now().Format("20060102"), "dhtnums")
				}
			}

			this.Redirect("/"+strings.ToUpper(strings.TrimSpace(meta.InfoHash)), 302)
		}
	}

	this.TplNames = "magnet.html"
}