Skip to content

Instantly share code, notes, and snippets.

@akhil-reni
Last active December 17, 2018 08:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akhil-reni/109c555c36cbb1d0e67b04d59e2e49ec to your computer and use it in GitHub Desktop.
Save akhil-reni/109c555c36cbb1d0e67b04d59e2e49ec to your computer and use it in GitHub Desktop.
A JSON Rest API for Subfinder
package main
/*
Usage:
go get github.com/Ice3man543/subfinder
go get github.com/gin-gonic/gin
go get github.com/jinzhu/gorm
go get github.com/mattn/go-sqlite3
go run api.go
Adding a task
=============
curl -i -X POST -H "Content-Type: application/json" -d "{ \"domain\": \"wesecureapp.com\"}" http://localhost:8080/api/v1/tasks/
Response
==========
{"success":true,"taskid":1}
Viewing a task
================
http://localhost:8080/api/v1/tasks/2/
Response (if task not completed)
============================
{"completed":false,"success":false}
Response (if task completed)
=============================
[{"id":9,"domain":"20-www.wesecureapp.com","taskid":2},{"id":10,"domain":"20.wesecureapp.com","taskid":2},{"id":11,"domain":"2B-www.wesecureapp.com","taskid":2},{"id":12,"domain":"3Awww.wesecureapp.com","taskid":2},{"id":13,"domain":"blog.wesecureapp.com","taskid":2},{"id":14,"domain":"careers.wesecureapp.com","taskid":2},{"id":15,"domain":"hostmaster.wesecureapp.com","taskid":2},{"id":16,"domain":"www.wesecureapp.com","taskid":2}]
*/
import (
"strconv"
"github.com/Ice3man543/subfinder/libsubfinder/engines/passive"
"github.com/Ice3man543/subfinder/libsubfinder/helper"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
type Tasks struct {
Id int `gorm:"AUTO_INCREMENT" form:"id" json:"id"`
Domain string `gorm:"not null" form:"firstname" json:"firstname"`
Completed bool `gorm:"default false" form:"completed" json:"completed"`
}
type Subdomains struct {
Id int `gorm:"AUTO_INCREMENT" form:"id" json:"id"`
Subdomain string `gorm:"not null" form:"domain" json:"domain"`
TaskId int `gorm:"not null" form:"taskid" json:"taskid"`
}
func InitDb() *gorm.DB {
// Openning file
db, err := gorm.Open("sqlite3", "./data.db")
// Display SQL queries
db.LogMode(true)
// Error
if err != nil {
panic(err)
}
// Creating the table Tasks
if !db.HasTable(&Tasks{}) {
db.CreateTable(&Tasks{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Tasks{})
}
// Creating the table Subdomains
if !db.HasTable(&Subdomains{}) {
db.CreateTable(&Subdomains{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Subdomains{})
}
return db
}
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
c.Next()
}
}
func RunSubfinder(state *helper.State, taskid int) {
db := InitDb()
defer db.Close()
domains := passive.Enumerate(state)
for _, domain := range domains {
var subdomain Subdomains
subdomain.Subdomain = domain
subdomain.TaskId = taskid
db.Create(&subdomain)
}
var task Tasks
db.First(&task, taskid)
task.Completed = true
db.Save(&task)
}
func CreateTask(c *gin.Context) {
db := InitDb()
defer db.Close()
var task Tasks
state, _ := helper.InitState()
c.BindJSON(&state)
if state.ComResolver == "" && state.ListResolver == "" {
state.LoadResolver = append(state.LoadResolver, "1.1.1.1")
state.LoadResolver = append(state.LoadResolver, "8.8.8.8")
state.LoadResolver = append(state.LoadResolver, "8.8.4.4")
}
if state.Sources == "" {
state.Sources = "all"
}
if state.Domain != "" {
task.Domain = state.Domain
db.Create(&task)
go RunSubfinder(&state, task.Id)
c.JSON(200, gin.H{"success": true, "taskid": task.Id})
} else {
c.JSON(400, gin.H{"success": false, "taskid": 0})
}
}
func GetDomains(c *gin.Context) {
db := InitDb()
defer db.Close()
taskid, err := strconv.Atoi(c.Params.ByName("taskid"))
if err != nil {
c.JSON(400, gin.H{"success": false})
}
var task Tasks
db.First(&task, taskid)
if task.Completed {
var subdomains []Subdomains
db.Where(&Subdomains{TaskId: taskid}).Find(&subdomains)
c.JSON(200, subdomains)
} else {
c.JSON(200, gin.H{"success": false, "completed": false})
}
}
func main() {
r := gin.Default()
r.Use(Cors())
v1 := r.Group("api/v1")
{
v1.POST("/tasks/", CreateTask)
v1.GET("/tasks/:taskid/", GetDomains)
}
r.Run(":8080")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment