support new starcrypto package

master v1.2.1
兔子 3 years ago
parent 2db6102668
commit 996f94eef0

@ -497,7 +497,11 @@ func (c *ClientCommon) Reply(m Message, value MsgVal) error {
} }
func (c *ClientCommon) ExchangeKey(newKey []byte) error { func (c *ClientCommon) ExchangeKey(newKey []byte) error {
newSendKey, err := starcrypto.RSAEncrypt(newKey, c.handshakeRsaPubKey) pubKey, err := starcrypto.DecodePublicKey(c.handshakeRsaPubKey)
if err != nil {
return err
}
newSendKey, err := starcrypto.RSAEncrypt(pubKey, newKey)
if err != nil { if err != nil {
return err return err
} }

@ -146,8 +146,13 @@ func (c *ClientConn) readTUMessage() {
} }
func (c *ClientConn) rsaDecode(message Message) { func (c *ClientConn) rsaDecode(message Message) {
unknownKey := message.Value privKey, err := starcrypto.DecodePrivateKey(c.handshakeRsaKey, "")
data, err := starcrypto.RSADecrypt(unknownKey, c.handshakeRsaKey, "") if err != nil {
fmt.Println(err)
message.Reply([]byte("failed"))
return
}
data, err := starcrypto.RSADecrypt(privKey, message.Value)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
message.Reply([]byte("failed")) message.Reply([]byte("failed"))

@ -14,22 +14,24 @@ func Test_ServerTuAndClientCommon(t *testing.T) {
noEn := func(key, bn []byte) []byte { noEn := func(key, bn []byte) []byte {
return bn return bn
} }
_ = noEn
server := NewServer() server := NewServer()
server.SetDefaultCommDecode(noEn) //server.SetDefaultCommDecode(noEn)
server.SetDefaultCommEncode(noEn) //server.SetDefaultCommEncode(noEn)
err := server.Listen("tcp", "127.0.0.1:12345") err := server.Listen("tcp", "127.0.0.1:12345")
if err != nil { if err != nil {
panic(err) panic(err)
} }
server.SetLink("notify", notify) server.SetLink("notify", notify)
for i := 1; i <= 5000; i++ { for i := 1; i <= 100; i++ {
go func() { go func() {
client := NewClient() client := NewClient()
client.SetMsgEn(noEn) //client.SetMsgEn(noEn)
client.SetMsgDe(noEn) //client.SetMsgDe(noEn)
client.SetSkipExchangeKey(true) //client.SetSkipExchangeKey(true)
err = client.Connect("tcp", "127.0.0.1:12345") err = client.Connect("tcp", "127.0.0.1:12345")
if err != nil { if err != nil {
t.Fatal(err)
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
return return
} }
@ -38,6 +40,7 @@ func Test_ServerTuAndClientCommon(t *testing.T) {
//nowd = time.Now().UnixNano() //nowd = time.Now().UnixNano()
client.SendWait("notify", []byte("client hello"), time.Second*15) client.SendWait("notify", []byte("client hello"), time.Second*15)
//client.Send("notify", []byte("client hello"))
//time.Sleep(time.Millisecond) //time.Sleep(time.Millisecond)
//fmt.Println("finished:", float64(time.Now().UnixNano()-nowd)/1000000) //fmt.Println("finished:", float64(time.Now().UnixNano()-nowd)/1000000)
//client.Send("notify", []byte("client")) //client.Send("notify", []byte("client"))
@ -65,7 +68,10 @@ func notify(msg *Message) {
} }
func Test_normal(t *testing.T) { func Test_normal(t *testing.T) {
server, _ := net.Listen("udp", "127.0.0.1:12345") server, err := net.Listen("tcp", "127.0.0.1:12345")
if err != nil {
t.Fatal(err)
}
go func() { go func() {
for { for {
conn, err := server.Accept() conn, err := server.Accept()
@ -87,7 +93,7 @@ func Test_normal(t *testing.T) {
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
for i := 1; i <= 100; i++ { for i := 1; i <= 100; i++ {
go func() { go func() {
conn, err := net.Dial("udp", "127.0.0.1:12345") conn, err := net.Dial("tcp", "127.0.0.1:12345")
if err != nil { if err != nil {
panic(err) panic(err)
} }

Loading…
Cancel
Save