func auth(s *tcp.TCPSession) bool { buff := s.ReadMessage() params := strings.Split(string(buff), " ") str0 := "config" db := C.SwitchDB((*C.char)(convert.String2C(str0))) if params[0] != "auth" { s.SendMessage([]byte("Please auth first!")) return false } r := C.Get(&(db.tIndex), (*C.char)(convert.String2C("passwd"))) if int(r.code) == 0 { passwd := []byte{} for i := 0; ; i++ { passwd = append(passwd, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i)))))) if passwd[i] == 0 { break } } if convert.Equal(passwd, []byte(params[1])) { s.SendMessage([]byte("Auth success")) return true } else { s.SendMessage([]byte("Auth fail")) return false } } else { s.SendMessage([]byte("Auth success")) return true } }
func main() { str0 := "monkey" C.CreateDB((*C.char)(convert.String2C(str0))) //创建基础数据库 str0 = "config" //配置数据库 C.CreateDB((*C.char)(convert.String2C(str0))) key := "passwd" //初始密码 data := "monkey" db := C.SwitchDB((*C.char)(convert.String2C(str0))) C.Set(&(*db).tIndex, (*C.char)(convert.String2C(key)), (convert.String2C(data))) servicePort := ":1517" tcpAddr, err := net.ResolveTCPAddr("tcp4", servicePort) if err != nil { panic(err) } l, err := net.ListenTCP("tcp", tcpAddr) //侦听TCP if err != nil { panic(err) } for { conn, err := l.AcceptTCP() conn.SetKeepAlive(true) conn.SetNoDelay(true) if err != nil { panic(err) } go Handler(conn) } }
func Handler(s *tcp.TCPSession) { fmt.Println("Run a new handler to client.") for !auth(s) { } str := "monkey" db := C.SwitchDB((*C.char)(convert.String2C(str))) //环境变量-当前数据库 for { if s.Closed { fmt.Println("Close a client.") break } buff := s.ReadMessage() // if err != nil { // conn.Close() // break // } if len(buff) == 0 { fmt.Println("Close a client.") return } //commands := bytes.Split(buff,[]byte{0}) //for _,cmd := range commands { TranslateMessage2(s, &db, buff) //} //解析消息 } }
func Handler(conn *net.TCPConn) { str := "config" db := C.SwitchDB((*C.char)(convert.String2C(str))) //环境变量-当前数据库 for { buff := tcp.Receive(conn) //可能发生无限循环->已经ok,本机go编译器有问题 params := strings.Split(string(buff), " ") if params[0] != "auth" { tcp.Send(conn, []byte("Please auth first!")) continue } r := C.Get(&(*db).tIndex, (*C.char)(convert.String2C("passwd"))) if int(r.code) == 0 { passwd := []byte{} for i := 0; ; i++ { passwd = append(passwd, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i)))))) if passwd[i] == 0 { break } } if convert.Equal(passwd, []byte(params[1])) { tcp.Send(conn, []byte("Auth success")) break } else { tcp.Send(conn, []byte("Auth fail")) continue } } else { break } } str = "monkey" db = C.SwitchDB((*C.char)(convert.String2C(str))) //环境变量-当前数据库 for { buff := tcp.Receive(conn) // if err != nil { // conn.Close() // break // } if len(buff) == 0 { return } TranslateMessage(conn, &db, buff) //解析消息 } }
func initDB() { //初始化数据库 str0 := "monkey" C.CreateDB((*C.char)(convert.String2C(str0))) //创建基础数据库 str0 = "config" //配置数据库 C.CreateDB((*C.char)(convert.String2C(str0))) key := "passwd" //初始密码 data := "monkey" db := C.SwitchDB((*C.char)(convert.String2C(str0))) C.Set(&(db.tIndex), (*C.char)(convert.String2C(key)), (convert.String2C(data))) }
func (cmd command) Switchdb(db **C.Database) []byte { response := []byte{} key, _ := convert.ParseUntil(cmd, 0, 9) d := C.SwitchDB((*C.char)(convert.Bytes2C(key))) if d != nil { *db = d response = []byte("ok\n") } else { response = []byte("fail\n") } return response }
////////////////////////////////////////Dumplated////////////////////////////////////////////////////////////////////////// func TranslateMessage(s *tcp.TCPSession, db **C.Database, message []byte) { command := string(message) params := strings.Split(command, " ") //fmt.Println(params) response := []byte{} if params[0] == "set" { r := C.Set(&(*db).tIndex, (*C.char)(convert.String2C(params[1])), (convert.String2C(params[2]))) for i := 0; ; i++ { response = append(response, byte(r.msg[i])) if response[i] == 0 { break } } } else if params[0] == "get" { r := C.Get(&(*db).tIndex, (*C.char)(convert.String2C(params[1]))) // for i := 0;;i++ { // response = append(response,byte(r.msg[i])) // if response[i] == 0 { break; } // } if int(r.code) == 0 { for i := 0; ; i++ { response = append(response, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i)))))) if response[i] == 0 { break } } } else { // for i := 0;;i++ { // response = append(response,byte(r.msg[i])) // if response[i] == 0 { break; } // } } } else if params[0] == "delete" || params[0] == "remove" { r := C.Delete(&(*db).tIndex, (*C.char)(convert.String2C(params[1]))) for i := 0; ; i++ { response = append(response, byte(r.msg[i])) if response[i] == 0 { break } } } else if params[0] == "createdb" { d := C.CreateDB((*C.char)(convert.String2C(params[1]))) if d != nil { *db = d response = []byte("Already exist,switched\n") } else { response = []byte("Created\n") } } else if params[0] == "switchdb" { d := C.SwitchDB((*C.char)(convert.String2C(params[1]))) if d != nil { *db = d response = []byte("ok\n") } else { response = []byte("fail\n") } } else if params[0] == "dropdb" { *db = C.DropDB((*C.char)(convert.String2C(params[1]))) } else if strings.EqualFold("listdb", params[0]) { r := C.ListDB() for i := 0; i < 1024; i++ { b := byte(*(*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(r)) + uintptr(i)))) response = append(response, b) if b == 0 { break } } C.free(unsafe.Pointer(r)) } else { //fmt.Println("unkown command:",params[0]) } s.SendMessage(response) }