Esempio n. 1
0
func StartAuth() {
	gob.Register(make(map[string]string))
	authCache = cache.NewLRUCache(&cache.LRUOptions{Capacity: 128, MaxAge: time.Hour * 1})

	cfgCtrl := srv.Cfg().Get("server")
	sessionkey := cfgCtrl.Get("sessionkey").String()

	http.HandleFunc("/authorize", handleAuthorize)
	http.Handle("/oauth2callback", seshcookie.NewSessionHandler(
		&AuthHandler{},
		sessionkey, nil))

	cfgAU, _ := srv.Cfg().Get("authusers").Array()
	for i := 0; i < len(cfgAU); i++ {
		au := cfgAU[i].(map[string]interface{})
		id := au["id"].(string)
		name := au["name"].(string)
		log.Println("Authorized:", id, name)
		authUsers = append(authUsers, id)
	}

	port := cfgCtrl.Get("port").String()
	addr := cfgCtrl.Get("addr").String()
	if len(redirectAddr) > 0 {
		addr = redirectAddr
	}

	oauthCfg.RedirectURL = "http://" + addr + ":" + port + "/oauth2callback"

	table, _ = db.Main.InitTable(OAuth{})
}
Esempio n. 2
0
func Start() {
	timeStart = time.Now()
	templates = template.Must(template.ParseFiles("www/index.html"))

	cfgCtrl := srv.Cfg().Get("server")
	sessionkey := cfgCtrl.Get("sessionkey").String()

	http.Handle("/rpc/", seshcookie.NewSessionHandler(&RpcHandler{}, sessionkey, nil))
	http.Handle("/upload", seshcookie.NewSessionHandler(&UploadHandler{}, sessionkey, nil))
	//http.HandleFunc("/vfile/", vfileHandler)
	http.Handle("/", seshcookie.NewSessionHandler(&WebHandler{}, sessionkey, nil))
	StartAuth()

	port := cfgCtrl.Get("port").String()
	if len(port) > 0 {
		srv.HttpListenAndServeAsync(":"+port, nil, "", "", "")
	}
	tlsport := cfgCtrl.Get("tlsport").String()
	if len(tlsport) > 0 && tlsport != "null" {
		srv.HttpListenAndServeAsync(":"+tlsport, nil, "tls_cert.pem", "tls_key.pem", "")
	}

	//srv.VersionDataDir = cfgCtrl.Get("data").String()
	//srv.DoRefreshVersions()
}
Esempio n. 3
0
func (h *WebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sURL := r.URL.Path[1:]

	if sURL == "logout" {
		session := seshcookie.Session.Get(r)
		for k := range session {
			delete(session, k)
		}
		http.Redirect(w, r, "/", http.StatusFound)
		return
	}

	oa := CheckAuthorized(w, r, true)
	if oa == nil {
		return
	}

	if sURL == "" {
		sURL = "index.html"
	}
	//log.Println("web:", sURL)

	if templates.Lookup(sURL) != nil {
		ver := srv.Cfg().Get("version").String()
		p := &Page{Version: ver, UserName: oa.Name}
		renderTemplate(w, sURL, p)
		return
	}

	http.ServeFile(w, r, "www/"+sURL)
}
Esempio n. 4
0
func StartWeb() {
	http.HandleFunc("/rpc/", rpcHandler)

	cfgCtrl := srv.Cfg().Get("server")
	port := cfgCtrl.Get("port").String()
	if len(port) > 0 {
		srv.HttpListenAndServeAsync(":"+port, nil, "", "", "")
	}
	tlsport := cfgCtrl.Get("tlsport").String()
	if len(tlsport) > 0 && tlsport != "null" {
		srv.HttpListenAndServeAsync(":"+tlsport, nil, "tls_cert.pem", "tls_key.pem", "")
	}
}
Esempio n. 5
0
func ServerStart() (err error) {
	if srvCmd != nil {
		return errors.New("already_running")
	}
	srvToken = util.RandString(16)
	srvCmd = exec.Command("../gamesrv/gamesrv", "-token="+srvToken)
	stderr, _ := srvCmd.StderrPipe()
	err = srvCmd.Start()
	if err != nil {
		srvCmd = nil
		return errors.New("startup_error:" + err.Error())
	}
	srvStart = time.Now()
	srvQuit = make(chan bool)
	log.Println("Startet:", srvStart)

	cfgCtrl := srv.Cfg().Get("server")
	srvPort = cfgCtrl.Get("gameport").String()

	logfile := "../gamesrv/log/log.txt"
	for i := 0; i < 400; i++ {
		time.Sleep(10 * time.Millisecond)
		srvLogFile, err = os.Open(logfile)
		if err == nil {
			break
		}
	}
	if err != nil {
		log.Println("error opening logfile: ", err)
		srvCmd.Process.Signal(os.Kill)
		srvCmd = nil
		return errors.New("startup_error:" + err.Error())
	}

	go func() {
		srvRunning = true
		srvWait.Add(1) //wait for logfile
		defer srvWait.Done()

		pp := perf.NewProcPerf(srvCmd.Process)
		fi := bufio.NewReader(srvLogFile)
		er := bufio.NewReader(stderr)
		pr := srvCmd.Process

		tickLog := time.NewTicker(250 * time.Millisecond)
		tickPro := time.NewTicker(1000 * time.Millisecond)
		tickSta := time.NewTicker(5000 * time.Millisecond)
		defer tickLog.Stop()
		defer tickPro.Stop()
		defer tickSta.Stop()

		statsPending := false

		go func() {
			for {
				ln, err := er.ReadString('\n')
				if err == io.EOF {
					log.Println("err closed by server")
					break
				}
				if err != nil {
					log.Println("err closed by server:", err)
					break
				}
				if len(ln) > 0 && ln[len(ln)-1] == '\n' {
					ln = ln[:len(ln)-1]
				}
				//log.Print(ln)
				srv.PushLog("e==> " + ln)
			}
		}()

		for {
			select {
			case <-tickLog.C:
				for {
					ln, err := fi.ReadString('\n')
					if err == io.EOF {
						break
					}
					if err != nil {
						log.Println("log closed by server:", err)
						fi = nil
						srvRunning = false
						return
					}
					if len(ln) > 0 && ln[len(ln)-1] == '\n' {
						ln = ln[:len(ln)-1]
					}
					//log.Print(ln)
					srv.PushLog("l==> " + ln)
				}
				if pr != nil && srvCmd != nil {
					e, err := perf.GetExitCodeProcess(pr)
					if err != nil || e != 259 {
						log.Println("exit:", e, err)
						pr = nil
						go ServerStop()
						if GetAutoRestart() {
							go func() {
								time.Sleep(time.Millisecond * 8000)
								log.Println("auto restart")
								ServerStart()
							}()
						}
						continue
					}
				}
			case <-tickSta.C:

				go func() {
					if statsPending {
						return
					}
					statsPending = true
					stats, _ := ServerStats()
					statsPending = false
					if stats != nil && stats["requestCount"] != nil {
						srvRequestCount = int64(stats["requestCount"].(float64))
						srvRequestErr = int64(stats["requestErr"].(float64))
						srvRequestDuration = int64(stats["requestDuration"].(float64))
						srv.SrvRequestCount = srvRequestCount
						srv.SrvRequestDur = srvRequestDuration
					}
				}()
			case <-tickPro.C:
				err = pp.Get()
				srvLoadSys = pp.LoadSys
				srvLoadTotal = pp.LoadTotal
				srvWorkingSet = pp.WorkingSet
			case <-srvQuit:
				//fmt.Println("quit")

				s := srvLogFile.Name()
				srvLogFile.Close()
				srvLogFile = nil
				os.Remove(s)
				log.Println("log closed")
			}
		}

	}()

	return nil
}