Example #1
0
File: db.go Project: ZeaLoVe/hbs
func Init() {
	var err error
	DB, err = sql.Open("mysql", g.Config().Database)
	if err != nil {
		log.Fatalln("open db fail:", err)
	}

	DB.SetMaxIdleConns(g.Config().MaxIdle)

	err = DB.Ping()
	if err != nil {
		log.Fatalln("ping db fail:", err)
	}
}
Example #2
0
File: http.go Project: ZeaLoVe/hbs
func Start() {
	if !g.Config().Http.Enabled {
		return
	}

	addr := g.Config().Http.Listen
	if addr == "" {
		return
	}
	s := &http.Server{
		Addr:           addr,
		MaxHeaderBytes: 1 << 30,
	}
	log.Println("http listening", addr)
	log.Fatalln(s.ListenAndServe())
}
Example #3
0
func configCommonRoutes() {
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	})

	http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(g.VERSION))
	})

	http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, file.SelfDir())
	})

	http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.RemoteAddr, "127.0.0.1") {
			g.ParseConfig(g.ConfigFile)
			RenderDataJson(w, g.Config())
		} else {
			w.Write([]byte("no privilege"))
		}
	})
}
Example #4
0
File: rpc.go Project: ZeaLoVe/hbs
func Start() {
	addr := g.Config().Listen

	server := rpc.NewServer()
	// server.Register(new(filter.Filter))
	server.Register(new(Agent))
	server.Register(new(Hbs))

	l, e := net.Listen("tcp", addr)
	if e != nil {
		log.Fatalln("listen error:", e)
	} else {
		log.Println("listening", addr)
	}

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Println("listener accept fail:", err)
			continue
		}
		go server.ServeCodec(jsonrpc.NewServerCodec(conn))
	}
}
Example #5
0
File: proc.go Project: ZeaLoVe/hbs
func configProcRoutes() {
	http.HandleFunc("/expressions", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, cache.ExpressionCache.Get())
	})

	http.HandleFunc("/plugins/", func(w http.ResponseWriter, r *http.Request) {
		hostname := r.URL.Path[len("/plugins/"):]
		RenderDataJson(w, cache.GetPlugins(hostname))
	})

	//API get host_id by host_name
	http.HandleFunc("/hosts/id", func(w http.ResponseWriter, r *http.Request) {
		var host ResponseHostId
		host.Name = r.FormValue("name")
		if host.Name == "" {
			RenderMsgJson(w, "Not param")
			return
		}
		host_id, exist := cache.HostMap.GetID(host.Name)
		if !exist {
			RenderMsgJson(w, "name not in cache")
			return
		}
		host.HostId = host_id
		RenderJson(w, host)
	})

	//API get endpoint by name
	http.HandleFunc("/endpoint", func(w http.ResponseWriter, r *http.Request) {
		var res ResponseEndpoints
		var host ResponseHost
		host.Ip = r.FormValue("ip")
		if host.Ip == "" {
			RenderMsgJson(w, "Not param")
			return
		}
		target_ip := host.Ip
		if !isPrivateIP(target_ip) {
			//转化成内网IP
			target_ip = PrivateIP(host.Ip, g.Config().Nat)
		}
		host.Endpoint, _ = db.QueryEndpoint(target_ip)
		res.Items = append(res.Items, host)
		RenderJson(w, res)
	})

	http.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
		//body also response
		var body ResponseEndpoints
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)

		if err != nil {
			RenderMsgJson(w, "Not param, may be with wrong format")
			return
		}

		for i, _ := range body.Items {
			if body.Items[i].Ip == "" {
				continue
			}
			target_ip := body.Items[i].Ip
			if !isPrivateIP(target_ip) {
				//转化成内网IP
				target_ip = PrivateIP(body.Items[i].Ip, g.Config().Nat)
			}
			body.Items[i].Endpoint, _ = db.QueryEndpoint(target_ip)
		}
		RenderJson(w, body)
	})

	//老API重定向
	http.HandleFunc("/all/hosts", func(w http.ResponseWriter, r *http.Request) {

		http.Redirect(w, r, "/host/all", 302)
	})

	//get ,API of all hosts, use in agent alive check.
	http.HandleFunc("/host/all", func(w http.ResponseWriter, r *http.Request) {
		var hosts []ResponseHost
		var host ResponseHost
		cache.HostMap.Lock()
		//cache中的map的key就是hostname,也就是endpoint;value是hostid没用
		for key, _ := range cache.HostMap.M {
			host.Endpoint = key
			host.Ip = cache.HostMap.M2[key] //通过hostname找IP
			if strings.EqualFold(host.Ip, "0.0.0.0") {
				continue
			}
			hosts = append(hosts, host)
		}
		cache.HostMap.Unlock()
		RenderJson(w, hosts)
	})

	//API add host:GET
	http.HandleFunc("/host/add", func(w http.ResponseWriter, r *http.Request) {
		var args model.AgentReportRequest
		args.Hostname = r.FormValue("name")
		args.IP = r.FormValue("ip")
		args.AgentVersion = r.FormValue("agentversion")
		args.PluginVersion = r.FormValue("pluginversion")
		if args.Hostname == "" {
			RenderMsgJson(w, "require host name")
			return
		}
		if len(args.Hostname) > 255 {
			RenderMsgJson(w, "host name too long")
			return
		}
		if args.IP == "" {
			RenderMsgJson(w, "require host ip")
			return
		}
		cache.Agents.Put(&args)
		RenderMsgJson(w, "add Host done.")
	})

	//get ,API of all virtual hosts.ip=0.0.0.0
	http.HandleFunc("/vhost/all", func(w http.ResponseWriter, r *http.Request) {
		var hosts []ResponseHost
		var host ResponseHost
		cache.HostMap.Lock()
		//cache中的map的key就是hostname,也就是endpoint;value是hostid没用
		for key, _ := range cache.HostMap.M {
			host.Endpoint = key
			host.Ip = cache.HostMap.M2[key] //通过hostname找IP
			if strings.EqualFold(host.Ip, "0.0.0.0") {
				hosts = append(hosts, host)
			}
		}
		cache.HostMap.Unlock()
		RenderJson(w, hosts)
	})

	//API add virtual host:GET
	http.HandleFunc("/vhost/add", func(w http.ResponseWriter, r *http.Request) {
		var args model.AgentReportRequest
		args.Hostname = r.FormValue("name")
		args.IP = "0.0.0.0"
		args.AgentVersion = "0.0.0"
		args.PluginVersion = "0.0.0"
		if args.Hostname == "" {
			RenderMsgJson(w, "require vhost name")
			return
		}
		if len(args.Hostname) > 255 {
			RenderMsgJson(w, "vhost name too long")
			return
		}
		cache.HostMap.Lock()
		_, exist := cache.HostMap.M2[args.Hostname]
		cache.HostMap.Unlock()
		if exist {
			RenderMsgJson(w, "vHost exist.")
		} else {
			cache.Agents.Put(&args)
			RenderMsgJson(w, "add vHost done.")
		}
	})

}
Example #6
0
File: agent.go Project: ZeaLoVe/hbs
// 需要checksum一下来减少网络开销?其实白名单通常只会有一个或者没有,无需checksum
func (t *Agent) TrustableIps(args *model.NullRpcRequest, ips *string) error {
	*ips = strings.Join(g.Config().Trustable, ",")
	return nil
}