Example #1
0
func BlockPackagePost(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksList"] = true

	keys, err := models.BlockPackage(ctx.Query("import_path"), ctx.Query("note"))
	if err != nil {
		if err == models.ErrPackageNotExist {
			ctx.RenderWithErr(err.Error(), "blocks/new", nil)
		} else {
			ctx.Handle(500, "BlockPackage", err)
		}
		return
	}

	if setting.ProdMode {
		for _, k := range keys {
			log.Trace("Deleting archive: %s", k)
			if err = qiniu.DeleteArchive(k); err != nil {
				log.Error(4, "Fail to delete archive(%s): %v", k, err)
			}
			log.Info("Archive deleted: %s", k)
		}
	}

	ctx.Flash.Success("New package has been blocked!")
	ctx.Redirect("/admin/blocks")
}
Example #2
0
func cleanExpireRevesions() {
	if err := x.Where("updated<?", time.Now().Add(_EXPIRE_DURATION)).
		Iterate(new(Revision), func(idx int, bean interface{}) (err error) {
			rev := bean.(*Revision)
			if err = rev.GetPackage(); err != nil {
				return err
			}

			if _, err = x.Id(rev.Id).Delete(new(Revision)); err != nil {
				return err
			}

			ext := archive.GetExtension(rev.Pkg.ImportPath)
			fpath := path.Join(setting.ArchivePath, rev.Pkg.ImportPath, rev.Revision+ext)

			switch rev.Storage {
			case LOCAL:
				os.Remove(fpath)
				log.Info("Revision deleted: %s", fpath)
				return nil
			case QINIU:
				key, err := rev.KeyName()
				if err != nil {
					return err
				}
				if setting.ProdMode {
					if err = qiniu.DeleteArchive(key); err != nil {
						return err
					}
				}
				log.Info("Revision deleted: %s", key)
				return nil
			default:
				return nil
			}

			return nil
		}); err != nil {
		log.Error(3, "Fail to clean expire revisions: %v", err)
	}
}
Example #3
0
// RunBlockRule applies given block rule to all packages.
func RunBlockRule(id int64) (count int64, keys []string, err error) {
	r, err := GetBlockRuleById(id)
	if err != nil {
		return 0, nil, err
	}
	exp, err := regexp.Compile(r.Rule)
	if err != nil {
		return 0, nil, err
	}

	keys = make([]string, 0, 10)

	err = x.Iterate(new(Package), func(idx int, bean interface{}) error {
		pkg := bean.(*Package)

		if !exp.MatchString(pkg.ImportPath) {
			return nil
		}

		revs, err := pkg.GetRevisions()
		if err != nil {
			return fmt.Errorf("error getting revisions(%s): %v", pkg.ImportPath, err)
		}

		// Delete package archives.
		ext := archive.GetExtension(pkg.ImportPath)
		for _, rev := range revs {
			switch rev.Storage {
			case QINIU:
				keys = append(keys, pkg.ImportPath+"-"+rev.Revision+ext)
			}

			if _, err = x.Id(rev.Id).Delete(new(Revision)); err != nil {
				return fmt.Errorf("error deleting revision(%s-%s): %v", pkg.ImportPath, rev.Revision, err)
			}
		}
		os.RemoveAll(path.Join(setting.ArchivePath, pkg.ImportPath))

		if setting.ProdMode {
			if _, err = x.Id(pkg.Id).Delete(new(Package)); err != nil {
				return fmt.Errorf("error deleting package(%s): %v", pkg.ImportPath, err)
			}
		}

		log.Info("[%d] Package blocked: %s", r.Id, pkg.ImportPath)

		count++
		return nil
	})

	return count, keys, err
}
Example #4
0
func RunRule(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksRules"] = true

	rid := ctx.ParamsInt64(":id")
	count, keys, err := models.RunBlockRule(rid)
	if err != nil {
		ctx.Handle(500, "RunBlockRule", err)
		return
	}

	if setting.ProdMode {
		for _, k := range keys {
			log.Trace("Deleting archive: %s", k)
			if err = qiniu.DeleteArchive(k); err != nil {
				log.Error(4, "Fail to delete archive(%s): %v", k, err)
			}
			log.Info("Archive deleted: %s", k)
		}
	}

	ctx.Flash.Success(fmt.Sprintf("%d packages are blocked by rule ID: %d.", count, rid))
	ctx.Redirect("/admin/blocks/rules")
}
Example #5
0
func main() {
	log.Info("%s %s", setting.AppName, APP_VER)
	log.Info("Run Mode: %s", strings.Title(macaron.Env))

	m := macaron.New()
	m.Use(macaron.Logger())
	m.Use(macaron.Recovery())
	m.Use(macaron.Static("public", macaron.StaticOptions{
		SkipLogging: true,
	}))
	m.Use(pongo2.Pongoers(pongo2.Options{
		Directory:  "templates/web",
		IndentJSON: macaron.Env != macaron.PROD,
	}, "templates/admin"))
	m.Use(i18n.I18n())
	m.Use(session.Sessioner())
	m.Use(middleware.Contexter())

	// Routes.
	m.Get("/", routers.Home)
	m.Route("/download", "GET,POST", routers.Download)
	m.Get("/favicon.ico", func(ctx *middleware.Context) {
		ctx.Redirect("/img/favicon.png")
	})
	// m.Get("/search", routers.Search)
	// m.Get("/about", routers.About)

	// Package.
	m.Get("/*", routers.Package)
	m.Get("/badge/*", routers.Badge)

	// Admin.
	m.Post("/admin/auth", admin.AuthPost)
	m.Group("/admin", func() {
		m.Get("", admin.Dashboard)

		m.Group("/packages", func() {
			m.Get("", admin.Revisions)
			m.Get("/larges", admin.LargeRevisions)
		})

		m.Group("/blocks", func() {
			m.Get("", admin.Blocks)
			m.Combo("/new").Get(admin.BlockPackage).Post(admin.BlockPackagePost)
			m.Get("/:id:int/delete", admin.UnblockPackage)

			m.Group("/rules", func() {
				m.Get("", admin.BlockRules)
				m.Combo("/new").Get(admin.NewBlockRule).Post(admin.NewBlockRulePost)
				m.Get("/:id:int/run", admin.RunRule)
				m.Get("/:id:int/delete", admin.DeleteBlockRule)
			})
		})
	}, admin.Auth)

	// API.
	m.Group("/api", func() {
		m.Group("/v1", func() {
			m.Group("", func() {
				m.Get("/download", v1.Download)
				m.Get("/revision", v1.GetRevision)
			}, v1.PackageFilter())
		})
	})

	// Robots.txt
	m.Get("/robots.txt", func() string {
		return `User-agent: *
Disallow: /api/
Disallow: /download`
	})

	m.NotFound(routers.NotFound)

	listenAddr := fmt.Sprintf("0.0.0.0:%d", setting.HttpPort)
	log.Info("Listen: http://%s", listenAddr)
	if err := http.ListenAndServe(listenAddr, m); err != nil {
		log.Fatal(4, "Fail to start server: %v", err)
	}
}
Example #6
0
// uploadArchives checks and uploads local archives to QiNiu.
func uploadArchives() {
	revs, err := GetLocalRevisions()
	if err != nil {
		log.Error(4, "Fail to get local revisions: %v", err)
		return
	}

	// Upload.
	for _, rev := range revs {
		pkg, err := GetPakcageById(rev.PkgId)
		if err != nil {
			log.Error(4, "Fail to get package by ID(%d): %v", rev.PkgId, err)
			continue
		}

		ext := archive.GetExtension(pkg.ImportPath)
		key := pkg.ImportPath + "-" + rev.Revision + ext
		localPath := path.Join(pkg.ImportPath, rev.Revision)
		fpath := path.Join(setting.ArchivePath, localPath+ext)

		// Move.
		// rsCli := rs.New(nil)
		// log.Info(key)
		// err = rsCli.Move(nil, setting.BucketName, pkg.ImportPath+"-"+rev.Revision, setting.BucketName, key)
		// if err != nil {
		// 	log.Error(4, rev.Revision)
		// }
		// continue

		if !com.IsFile(fpath) {
			log.Debug("Delete: %v", fpath)
			DeleteRevisionById(rev.Id)
			continue
		}

		// Check archive size.
		f, err := os.Open(fpath)
		if err != nil {
			log.Error(4, "Fail to open file(%s): %v", fpath, err)
			continue
		}
		fi, err := f.Stat()
		if err != nil {
			log.Error(4, "Fail to get file info(%s): %v", fpath, err)
			continue
		}
		// Greater then MAX_UPLOAD_SIZE.
		if fi.Size() > setting.MaxUploadSize<<20 {
			log.Debug("Ignore large archive: %v", fpath)
			continue
		}

		log.Debug("Uploading: %s", localPath)
		if err = qiniu.UploadArchive(key, fpath); err != nil {
			log.Error(4, "Fail to upload file(%s): %v", fpath, err)
			continue
		}
		rev.Storage = QINIU
		if err := UpdateRevision(rev); err != nil {
			log.Error(4, "Fail to upadte revision(%d): %v", rev.Id, err)
			continue
		}
		os.Remove(fpath)
		log.Info("Uploaded: %s", localPath)
	}
}