Skip to content

Commit 902bfd7

Browse files
fstreunJordiSubira
andauthored
sgrpc: Init gRPC over SCION/QUIC example (#236)
* sgrpc: Init gRPC over SCION/QUIC example * README: add gRPC over SCION/QUIC example * revision * linters Co-authored-by: Jordi Subirà Nieto <[email protected]>
1 parent f61bba1 commit 902bfd7

File tree

8 files changed

+430
-0
lines changed

8 files changed

+430
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ The directory _examples contains examples for the usage of the SCION libraries.
101101
Example for the use of QUIC over SCION.
102102
* [_examples/hellodrkey](_examples/hellodrkey/README.md):
103103
Example for the the use of DRKey.
104+
* [_examples/sgrpc](_examples/sgrpc/README.md):
105+
Example for using gRPC over SCION/QUIC with the PAN library.
104106
* [_examples/shttp](_examples/shttp/README.md):
105107
Examples for using HTTP over SCION/QUIC, examples for servers, proxies, and clients.
106108

_examples/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module examples
33
go 1.16
44

55
require (
6+
github.com/golang/protobuf v1.5.2
67
github.com/gorilla/handlers v1.5.1
78
github.com/lucas-clemente/quic-go v0.23.0
89
github.com/netsec-ethz/scion-apps v0.5.1-0.20220504120040-79211109ed3f

_examples/sgrpc/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Example for gRPC over SCION/QUIC
2+
3+
This directory contains a small example program that shows how gRPC can be used over SCION/QUIC with the PAN library.
4+
The example consists of an echo server and a client that sends a message to the service.
5+
6+
## Running
7+
8+
The example requires a running SCION endhost stack, i.e. a running SCION dispatcher and SCION daemon. Please refer to '[Running](../../README.md#Running)' in this repository's main README and the [SCIONLab tutorials](https://docs.scionlab.org) to get started.
9+
See '[Environment](../../README.md#Environment)' on how to set the dispatcher and sciond environment variables when e.g. running multiple local ASes.
10+
11+
To test the server and client, run the SCION tiny test topology.
12+
13+
Open a shell and run the server in the AS `1-ff00:0:111`:
14+
```bash
15+
# Server in 1-ff00:0:111
16+
SCION_DAEMON_ADDRESS="127.0.0.20:30255" \
17+
go run server/main.go --server-address 127.0.0.1:5000
18+
```
19+
20+
Open a shell and run the client in the AS `1-ff00:0:112` and send a message to the server:
21+
```bash
22+
# Client in 1-ff00:0:112
23+
SCION_DAEMON_ADDRESS="127.0.0.28:30255" \
24+
go run client/main.go --server-address "1-ff00:0:111,127.0.0.1:5000" --message "gRPC over SCION/QUIC"
25+
```
26+
27+
## Protobuf
28+
Tutorial: https://grpc.io/docs/languages/go/basics/
29+
30+
Requirements:
31+
```bash
32+
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
33+
```
34+
35+
The compiler plugin protoc-gen-go will be installed in $GOBIN, defaulting to $GOPATH/bin. It must be in your $PATH for the protocol compiler protoc to find it.
36+
```bash
37+
export GO_PATH=~/go
38+
export PATH=$PATH:/$GO_PATH/bin
39+
```
40+
41+
The generation of the gRPC client and server interface is performed as follows:
42+
```bash
43+
protoc --go_out=. --go_opt=paths=source_relative \
44+
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
45+
proto/*.proto
46+
```

_examples/sgrpc/client/main.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"flag"
7+
"fmt"
8+
"log"
9+
"net"
10+
"time"
11+
12+
"github.com/lucas-clemente/quic-go"
13+
"github.com/netsec-ethz/scion-apps/pkg/pan"
14+
"github.com/netsec-ethz/scion-apps/pkg/quicutil"
15+
"google.golang.org/grpc"
16+
"google.golang.org/grpc/credentials/insecure"
17+
"inet.af/netaddr"
18+
19+
pb "examples/sgrpc/proto"
20+
)
21+
22+
var (
23+
Message = flag.String("message", "", "Message to send to the gRPC echo server")
24+
ServerAddr = flag.String("server-addr", "1-ff00:0:111,127.0.0.1:5000", "Address of the echo server")
25+
)
26+
27+
func NewPanQuicDialer(tlsCfg *tls.Config) func(context.Context, string) (net.Conn, error) {
28+
dialer := func(ctx context.Context, addr string) (net.Conn, error) {
29+
panAddr, err := pan.ResolveUDPAddr(ctx, addr)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
clientQuicConfig := &quic.Config{KeepAlive: true}
35+
session, err := pan.DialQUIC(ctx, netaddr.IPPort{}, panAddr, nil, nil, "", tlsCfg, clientQuicConfig)
36+
if err != nil {
37+
return nil, fmt.Errorf("did not dial: %w", err)
38+
}
39+
return quicutil.NewSingleStream(session)
40+
}
41+
42+
return dialer
43+
}
44+
45+
func main() {
46+
flag.Parse()
47+
48+
tlsCfg := &tls.Config{
49+
InsecureSkipVerify: true,
50+
NextProtos: []string{"echo_service"},
51+
}
52+
53+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
54+
defer cancel()
55+
56+
grpcDial, err := grpc.DialContext(ctx, *ServerAddr,
57+
grpc.WithContextDialer(NewPanQuicDialer(tlsCfg)),
58+
grpc.WithTransportCredentials(insecure.NewCredentials()),
59+
)
60+
if err != nil {
61+
log.Fatalf("failed to dial gRPC: %v", err)
62+
}
63+
64+
c := pb.NewEchoServiceClient(grpcDial)
65+
66+
req := &pb.EchoRequest{
67+
Msg: *Message,
68+
}
69+
resp, err := c.Echo(ctx, req)
70+
if err != nil {
71+
log.Fatalf("gRPC did not connect: %v", err)
72+
}
73+
74+
fmt.Println(resp.Msg)
75+
}

_examples/sgrpc/proto/echo.pb.go

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/sgrpc/proto/echo.proto

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
syntax = "proto3";
2+
3+
package proto.echo;
4+
5+
option go_package = "examples/sgrpc/proto";
6+
7+
service EchoService{
8+
rpc Echo(EchoRequest) returns (EchoResponse) {}
9+
}
10+
11+
message EchoRequest{
12+
string msg = 1;
13+
}
14+
15+
message EchoResponse{
16+
string msg = 1;
17+
}

_examples/sgrpc/proto/echo_grpc.pb.go

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)