mirror of https://github.com/tidwall/redcon.git
tls example
This commit is contained in:
parent
4f3c759c8f
commit
f2794a0a30
136
README.md
136
README.md
|
@ -17,6 +17,7 @@ Features
|
||||||
- Simple interface. One function `ListenAndServe` and two types `Conn` & `Command`
|
- Simple interface. One function `ListenAndServe` and two types `Conn` & `Command`
|
||||||
- Support for pipelining and telnet commands
|
- Support for pipelining and telnet commands
|
||||||
- Works with Redis clients such as [redigo](https://github.com/garyburd/redigo), [redis-py](https://github.com/andymccurdy/redis-py), [node_redis](https://github.com/NodeRedis/node_redis), and [jedis](https://github.com/xetorthio/jedis)
|
- Works with Redis clients such as [redigo](https://github.com/garyburd/redigo), [redis-py](https://github.com/andymccurdy/redis-py), [node_redis](https://github.com/NodeRedis/node_redis), and [jedis](https://github.com/xetorthio/jedis)
|
||||||
|
- [TLS Support](#tls-example)
|
||||||
|
|
||||||
Installing
|
Installing
|
||||||
----------
|
----------
|
||||||
|
@ -25,7 +26,7 @@ Installing
|
||||||
go get -u github.com/tidwall/redcon
|
go get -u github.com/tidwall/redcon
|
||||||
```
|
```
|
||||||
|
|
||||||
Examples
|
Example
|
||||||
-------
|
-------
|
||||||
|
|
||||||
Here's a full example of a Redis clone that accepts:
|
Here's a full example of a Redis clone that accepts:
|
||||||
|
@ -123,136 +124,17 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The same example is also provided for serving redcon over TLS:
|
TLS Example
|
||||||
|
-----------
|
||||||
|
|
||||||
|
Redcon has full TLS support through the `ListenAndServeTLS` function.
|
||||||
|
|
||||||
|
The [same example](example/tls/clone.go) is also provided for serving Redcon over TLS.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go run example/tls/tls.go
|
go run example/tls/clone.go
|
||||||
```
|
```
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/tidwall/redcon"
|
|
||||||
)
|
|
||||||
|
|
||||||
const serverKey = `
|
|
||||||
-----BEGIN EC PARAMETERS-----
|
|
||||||
BggqhkjOPQMBBw==
|
|
||||||
-----END EC PARAMETERS-----
|
|
||||||
-----BEGIN EC PRIVATE KEY-----
|
|
||||||
MHcCAQEEIHg+g2unjA5BkDtXSN9ShN7kbPlbCcqcYdDu+QeV8XWuoAoGCCqGSM49
|
|
||||||
AwEHoUQDQgAEcZpodWh3SEs5Hh3rrEiu1LZOYSaNIWO34MgRxvqwz1FMpLxNlx0G
|
|
||||||
cSqrxhPubawptX5MSr02ft32kfOlYbaF5Q==
|
|
||||||
-----END EC PRIVATE KEY-----
|
|
||||||
`
|
|
||||||
|
|
||||||
const serverCert = `
|
|
||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIB+TCCAZ+gAwIBAgIJAL05LKXo6PrrMAoGCCqGSM49BAMCMFkxCzAJBgNVBAYT
|
|
||||||
AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn
|
|
||||||
aXRzIFB0eSBMdGQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xNTEyMDgxNDAxMTNa
|
|
||||||
Fw0yNTEyMDUxNDAxMTNaMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0
|
|
||||||
YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMM
|
|
||||||
CWxvY2FsaG9zdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHGaaHVod0hLOR4d
|
|
||||||
66xIrtS2TmEmjSFjt+DIEcb6sM9RTKS8TZcdBnEqq8YT7m2sKbV+TEq9Nn7d9pHz
|
|
||||||
pWG2heWjUDBOMB0GA1UdDgQWBBR0fqrecDJ44D/fiYJiOeBzfoqEijAfBgNVHSME
|
|
||||||
GDAWgBR0fqrecDJ44D/fiYJiOeBzfoqEijAMBgNVHRMEBTADAQH/MAoGCCqGSM49
|
|
||||||
BAMCA0gAMEUCIEKzVMF3JqjQjuM2rX7Rx8hancI5KJhwfeKu1xbyR7XaAiEA2UT7
|
|
||||||
1xOP035EcraRmWPe7tO0LpXgMxlh2VItpc2uc2w=
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
`
|
|
||||||
|
|
||||||
var addr = ":6380"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
cer, err := tls.X509KeyPair([]byte(serverCert), []byte(serverKey))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
config := &tls.Config{Certificates: []tls.Certificate{cer}}
|
|
||||||
|
|
||||||
var mu sync.RWMutex
|
|
||||||
var items = make(map[string][]byte)
|
|
||||||
|
|
||||||
go log.Printf("started server at %s", addr)
|
|
||||||
err = redcon.ListenAndServeTLS(addr,
|
|
||||||
func(conn redcon.Conn, cmd redcon.Command) {
|
|
||||||
switch strings.ToLower(string(cmd.Args[0])) {
|
|
||||||
default:
|
|
||||||
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
|
|
||||||
case "detach":
|
|
||||||
hconn := conn.Detach()
|
|
||||||
log.Printf("connection has been detached")
|
|
||||||
go func() {
|
|
||||||
defer hconn.Close()
|
|
||||||
hconn.WriteString("OK")
|
|
||||||
hconn.Flush()
|
|
||||||
}()
|
|
||||||
return
|
|
||||||
case "ping":
|
|
||||||
conn.WriteString("PONG")
|
|
||||||
case "quit":
|
|
||||||
conn.WriteString("OK")
|
|
||||||
conn.Close()
|
|
||||||
case "set":
|
|
||||||
if len(cmd.Args) != 3 {
|
|
||||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mu.Lock()
|
|
||||||
items[string(cmd.Args[1])] = cmd.Args[2]
|
|
||||||
mu.Unlock()
|
|
||||||
conn.WriteString("OK")
|
|
||||||
case "get":
|
|
||||||
if len(cmd.Args) != 2 {
|
|
||||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mu.RLock()
|
|
||||||
val, ok := items[string(cmd.Args[1])]
|
|
||||||
mu.RUnlock()
|
|
||||||
if !ok {
|
|
||||||
conn.WriteNull()
|
|
||||||
} else {
|
|
||||||
conn.WriteBulk(val)
|
|
||||||
}
|
|
||||||
case "del":
|
|
||||||
if len(cmd.Args) != 2 {
|
|
||||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mu.Lock()
|
|
||||||
_, ok := items[string(cmd.Args[1])]
|
|
||||||
delete(items, string(cmd.Args[1]))
|
|
||||||
mu.Unlock()
|
|
||||||
if !ok {
|
|
||||||
conn.WriteInt(0)
|
|
||||||
} else {
|
|
||||||
conn.WriteInt(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
func(conn redcon.Conn) bool {
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
func(conn redcon.Conn, err error) {
|
|
||||||
},
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
Benchmarks
|
Benchmarks
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue