Ejemplo n.º 1
0
func (this *LoginController) Http(data map[string]interface{}, w http.ResponseWriter, r *http.Request) {
	var sid string
	sess := &models.SessionTab{}

	sidObj, ok := data["sid"]
	if !ok {
		sid, _ = sess.CreateSession()
	} else {
		sid, ok = sidObj.(string)
		if !ok {
			result, _ := com.JsonEncode(map[string]interface{}{
				"result": false, //bool, login result
				"msg":    "invalid sid, sid must be a string.",
			})
			io.WriteString(w, result)
			return
		} else {
			s, err := sess.GetSession(sid)
			if err != nil {
				result, _ := com.JsonEncode(map[string]interface{}{
					"result": false, //bool, login result
					"msg":    "invalid sid, login failed.",
				})
				io.WriteString(w, result)
				return
			}

			if s.Session == sid {

			} else {
				result, _ := com.JsonEncode(map[string]interface{}{
					"result": false, //bool, login result
					"msg":    "invalid sid, please login without sid.",
				})
				io.WriteString(w, result)
				return
			}
		}
	}

	// login success
	result, _ := com.JsonEncode(map[string]interface{}{
		"result": true, //bool, login result
		"sid":    sid,
		"os":     runtime.GOOS + " " + runtime.GOARCH,
		"language": map[string]interface{}{ //language:compiler
			"C":    "gcc",
			"C++":  "g++",
			"Java": "javac version 1.7",
		},
		"time": 123456789, //server time stamp
	})
	io.WriteString(w, result)

}
Ejemplo n.º 2
0
func HandleJsonRpc(w http.ResponseWriter, r *http.Request) {
	// get request content
	p := make([]byte, r.ContentLength)
	r.Body.Read(p)

	content := string(p)

	log.Blueln(content)

	json, err := com.JsonDecode(content)

	if err != nil {
		log.Warnln("not json-rpc format")
		return
	}

	data := json.(map[string]interface{})

	// get system password
	password := C.Get("", "password")

	// parse received password
	passwordRecv, ok := data["password"].(string)
	if !ok {
		result, _ := com.JsonEncode(map[string]interface{}{
			"result": false, //bool, login result
			"msg":    "invalid password, password must be string.",
		})
		io.WriteString(w, result)
		return
	}

	// compare password
	if password != passwordRecv {
		result, _ := com.JsonEncode(map[string]interface{}{
			"result": false, //bool, login failed
		})
		io.WriteString(w, result)
		return
	}

	// trigger controller
	ctrl, exists := RouterMap[data["action"].(string)]
	if !exists {
		log.Warnln("not exist")
		return
	}
	ctrl.Http(data, w, r)
}
Ejemplo n.º 3
0
func (this *PingController) Tcp(data map[string]interface{}, cli *core.Client) {
	result, _ := com.JsonEncode(map[string]interface{}{
		"result": true,
		"msg":    "pong",
	})
	cli.Write(result)
}
Ejemplo n.º 4
0
// pack client message
func MsgPack(data map[string]interface{}) string {
	json, err := com.JsonEncode(data)
	if err != nil {
		return ""
	} else {
		return json
	}
}
Ejemplo n.º 5
0
func (this *PingController) Http(data map[string]interface{}, w http.ResponseWriter, r *http.Request) {
	result, _ := com.JsonEncode(map[string]interface{}{
		"result": true,
		"msg":    "pong",
	})

	io.WriteString(w, result)
}
Ejemplo n.º 6
0
func (this *TaskAddController) Http(data map[string]interface{}, w http.ResponseWriter, r *http.Request) {
	judge.AddTask(data)

	result, _ := com.JsonEncode(map[string]interface{}{
		"result": true, //bool, login result
		"msg":    "task added",
	})
	io.WriteString(w, result)
}
Ejemplo n.º 7
0
func (this *TaskAddController) Tcp(data map[string]interface{}, cli *core.Client) {
	judge.AddTask(data)

	result, _ := com.JsonEncode(map[string]interface{}{
		"result": true, //bool, login result
		"msg":    "task added",
	})
	cli.Write(result)
}
Ejemplo n.º 8
0
func GetLocation(ip string) string {
	json, _ := il.Location(ip)
	countryName := json["countryName"].(string)
	regionName := json["regionName"].(string)
	cityName := json["cityName"].(string)
	data := map[string]interface{}{
		"countryName": countryName,
		"regionName":  regionName,
		"cityName":    cityName,
	}
	str, _ := com.JsonEncode(data)
	return str
}
Ejemplo n.º 9
0
func (this *GatherController) Tcp(data map[string]interface{}, cli *core.Client) {
	info := &judge.Info{}
	sid := data["sid"].(string)
	id := data["id"].(float64)
	information := info.Gather(sid, int(id), core.C.Get(runtime.GOOS, "buildpath"))

	result, _ := com.JsonEncode(map[string]interface{}{
		"info": information,

		"time": 123456789,
		"sid":  sid,
		"id":   id,
	})
	cli.Write(result)
}
Ejemplo n.º 10
0
func GetLocation(ip string) (string, error) {
	json, err := il.Location(ip)
	if json == nil {
		return "", errors.New("json is nil")
	}
	countryName := json["countryName"].(string)
	regionName := json["regionName"].(string)
	cityName := json["cityName"].(string)
	data := map[string]interface{}{
		"countryName": countryName,
		"regionName":  regionName,
		"cityName":    cityName,
	}
	str, err := com.JsonEncode(data)
	return str, err
}
Ejemplo n.º 11
0
func (this *GatherController) Http(data map[string]interface{}, w http.ResponseWriter, r *http.Request) {

	info := &judge.Info{}
	sid := data["sid"].(string)
	id := data["id"].(float64)
	information := info.Gather(sid, int(id), core.C.Get(runtime.GOOS, "buildpath"))

	result, _ := com.JsonEncode(map[string]interface{}{
		"info": information,

		"time": 123456789,
		"sid":  sid,
		"id":   id,
	})

	io.WriteString(w, result)
}
Ejemplo n.º 12
0
// decode input and output data, "\n" seg
func DecodeIoData(inputs string, outputs string) (num int, json string, err error) {
	in := strings.Split(inputs, "\n")
	out := strings.Split(outputs, "\n")

	if len(in) > len(out) {
		num = len(out)
	} else {
		num = len(in)
	}

	ins := in[0:num]
	outs := out[0:num]

	data := map[string]interface{}{
		"input":   ins,
		"outputs": outs,
	}

	json, err = com.JsonEncode(data)

	return num, json, err
}
Ejemplo n.º 13
0
// send request
func (this *JClient) Request(msg map[string]interface{}) (map[string]interface{}, error) {
	msgStr, err := com.JsonEncode(msg)

	if err != nil {
		return nil, err
	}

	if this.conn == nil {
		log.Warnln("Connection Not Exist")
		return nil, errors.New("Connection Not Exist")
	}

	_, err = this.conn.Write([]byte(msgStr + this.mark))
	if err != nil {
		log.Warnln("[Write Error]", err)
		this.conn.Close()
		return nil, err
	}

	content, err := this.read()

	if err != nil {
		return nil, err
	}

	// kick sep char
	reg := regexp.MustCompile(this.mark)
	content = reg.ReplaceAllString(content, "")

	if this.debug {
		log.Bluef("[judger/send:%s]\n%s\n", time.Now(), msgStr)
		log.Warnf("[judger/recv:%s]\n%s\n", time.Now(), content)
	}

	resp, err := com.JsonDecode(content)

	return resp.(map[string]interface{}), err
}
Ejemplo n.º 14
0
func (this *LoginController) Tcp(data map[string]interface{}, cli *core.Client) {
	sess := &models.SessionTab{}
	password := core.C.Get("", "password")

	passwordRecv, ok := data["password"].(string)
	if !ok {
		result, _ := com.JsonEncode(map[string]interface{}{
			"result": false, //bool, login result
			"msg":    "invalid password, password must be string.",
		})
		cli.Write(result)
		return
	}
	if password != passwordRecv {
		result, _ := com.JsonEncode(map[string]interface{}{
			"result": false, //bool, login result
			"msg":    "wrong password.",
		})
		cli.Write(result)
		cli.Close()
		return
	}

	sid, ok := data["sid"]
	if ok {
		sidString, ok := sid.(string)
		if !ok {
			result, _ := com.JsonEncode(map[string]interface{}{
				"result": false, //bool, login result
				"msg":    "invalid sid, sid must be a string.",
			})
			cli.Write(result)
			return
		}

		s, err := sess.GetSession(sidString)
		if err == nil && s.Session == sidString {
			// login success
			cli.Login(true)
			result, _ := com.JsonEncode(map[string]interface{}{
				"result": true, //bool, login result
				"sid":    sid,
				"os":     runtime.GOOS + " " + runtime.GOARCH,
				"language": map[string]interface{}{ //language:compiler
					"C":    "gcc",
					"C++":  "g++",
					"Java": "javac version 1.7",
				},
				"time": 123456789, //server time stamp
			})
			cli.Write(result)
		} else {
			result, _ := com.JsonEncode(map[string]interface{}{
				"result": false, //bool, login result
				"msg":    "invalid sid, please login without sid.",
			})
			cli.Write(result)
		}
	} else {
		sid, err := sess.CreateSession()
		if err != nil {
			result, _ := com.JsonEncode(map[string]interface{}{
				"result": false, //bool, login result
				"msg":    "create session failed. please contact the admin.",
			})
			cli.Write(result)
		} else {
			cli.Login(true)
			result, _ := com.JsonEncode(map[string]interface{}{
				"result": true, //bool, login result
				"sid":    sid,
				"os":     runtime.GOOS + " " + runtime.GOARCH,
				"language": map[string]interface{}{ //language:compiler
					"C":    "gcc",
					"C++":  "g++",
					"Java": "javac version 1.7",
				},
				"time": 123456789, //server time stamp
			})
			cli.Write(result)
		}
	}

}