Skip to content

Latest commit

 

History

History
262 lines (201 loc) · 7.19 KB

README.adoc

File metadata and controls

262 lines (201 loc) · 7.19 KB

gRPC HelloWorld

Go

The original go grpc helloword come from https://github.com/grpc/grpc-go/tree/master/examples/helloworld.

Run Source Code on Local

1. Prepare Environment
tar -xvf go1.20.4.linux-amd64.tar.gz -C $HOME/.local
unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local

// set PATH
PATH=$PATH:$HOME/.local/go/bin
PATH=$PATH:$HOME/.local/bin

// plugin
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
2. start server
cd grpc-helloworld/go/
go run greeter_server/main.go
3. grpcurl
grpcurl -proto helloworld/helloworld.proto -v -plaintext -d '{"name":"go Tester"}' 10.1.10.205:50051 helloworld.Greeter/SayHello
4. run client
# go run greeter_client/main.go "Go Tester"
2023/06/06 09:40:23 Greeting: Hello Go Tester

Run Greetings on Docker

1. Greeting Server Build(code to image)
cd go/greeter_server/
go mod init cloudadc.github.io/greetings
go mod tidy
go build
cd ../greeter_client/
go mod init cloudadc.github.io/greetings-client
go mod tidy
go build

cd ..
docker build -t cloudadc/grpc-go-greeting:1.1 -f Dockerfile .
docker push cloudadc/grpc-go-greeting:1.1
2. start server
docker run -it --rm --name greetings -p 50051:50051  cloudadc/grpc-go-greeting:1.1
3. grpcurl
grpcurl -proto helloworld/helloworld.proto -v -plaintext -d '{"name":"go Tester"}' 10.1.10.205:50051 helloworld.Greeter/SayHello
4. greetings-client
~]# docker exec -it greetings greetings-client 127.0.0.1:50051
2023/06/06 04:06:13 Greeting: Hello World, What we achieve inwardly will change outer reality.

~]# docker exec -it greetings greetings-client 10.1.10.205:50051
2023/06/06 04:05:54 Greeting: Hello World, What we achieve inwardly will change outer reality.

~]# docker exec -it greetings greetings-client 10.1.10.205:50051 "Go Tester"
2023/06/06 04:07:26 Greeting: Hello Go Tester

Run Greetings on Docker(ARM64)

1. Greeting Server Build(code to image)
cd go/greeter_server/
go mod init cloudadc.github.io/greetings
go mod tidy
go build
cd ../greeter_client/
go mod init cloudadc.github.io/greetings-client
go mod tidy
go build

cd ..
sudo docker build -t cloudadc/grpc-go-greeting:1.1-aarch64 -f Dockerfile.arm .
sudo docker push cloudadc/grpc-go-greeting:1.1-aarch64
2. start server
sudo docker run -it --rm --name greetings -p 50051:50051 -d cloudadc/grpc-go-greeting:1.1-aarch64
3. greetings-client
$ sudo docker exec -it greetings greetings-client 127.0.0.1:50051
2023/06/06 04:31:41 Greeting: Hello World, What we achieve inwardly will change outer reality.
$ sudo docker exec -it greetings greetings-client 127.0.0.1:50051 "$(uname -a)"
2023/06/06 04:33:23 Greeting: Hello Linux instance-1 6.1.0-0.deb11.6-cloud-arm64 #1 SMP Debian 6.1.15-1~bpo11+1 (2023-03-16) aarch64 GNU/Linux

Run Greetings on K8S

Run Greetings(Lagecy) on Docker

1. start server
docker run -it --rm --name grpc-server-1 -e PORT=8009 -p 8009:8009 -d cloudadc/grpc-go-greeting:0.1 greeter_server
2. prepare env file
$ docker inspect grpc-server-1 | jq ".[0].NetworkSettings.IPAddress"
"172.17.0.2"
$ docker inspect grpc-server-2 | jq ".[0].NetworkSettings.IPAddress"
"172.17.0.3"

$ echo "ADDRESS=172.17.0.2:8009" > list/env-docker-1.list
$ echo "ADDRESS=172.17.0.3:8010" > list/env-docker-2.list
3. run client
$ docker run --env-file ./list/env-docker-1.list cloudadc/grpc-go-greeting:0.1 greeter_client "kylin SONG"
2023/02/28 01:21:04 Greeting: Hello kylin SONG

$ docker run --env-file ./list/env-docker-2.list cloudadc/grpc-go-greeting:0.1 greeter_client "kylin SONG"
2023/02/28 01:21:44 Greeting: Hello kylin SONG
4. run client with scripts
$ docker run --env-file ./list/env-docker-1.list cloudadc/grpc-go-greeting:0.1 greeter_client "This should be blocked <script>evil_script()</script>"
2023/02/28 01:27:42 Greeting: Hello This should be blocked <script>evil_script()</script>

$ docker run --env-file ./list/env-docker-2.list cloudadc/grpc-go-greeting:0.1 greeter_client "This should be blocked <script>evil_script()</script>"
2023/02/28 01:28:01 Greeting: Hello This should be blocked <script>evil_script()</script>

Run on Remote & Capture network packet flow

1. start server
$ docker run -it --rm --name grpc-server-1 -e PORT=8009 -p 8009:8009 -d cloudadc/grpc-go-greeting:0.1 greeter_server

~]# netstat -antulop | grep 8009
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN      1912/docker-proxy    off (0.00/0/0)
tcp6       0      0 :::8009                 :::*                    LISTEN      1916/docker-proxy    off (0.00/0/0)
2. prepare env file
echo "ADDRESS=10.1.20.204:8009" > list/env-remote.list
3. run client
$ docker run --env-file ./list/env-remote.list cloudadc/grpc-go-greeting:0.1 greeter_client
2023/02/28 02:14:01 Greeting: Hello world

$ docker run --env-file ./list/env-remote.list cloudadc/grpc-go-greeting:0.1 greeter_client "Kylin SONG"
2023/02/28 02:14:10 Greeting: Hello Kylin SONG

$ docker run --env-file ./list/env-remote.list cloudadc/grpc-go-greeting:0.1 greeter_client "This should be blocked <script>evil_script()</script>"
2023/02/28 02:14:28 Greeting: Hello This should be blocked <script>evil_script()</script>

4. Packet Overview

grpc go pcap

NGINX in front of gRPC Server

1. NGIINX Configration
upstream grpcgreeting_service {
    zone grpcgreeting_service 64k;
    server 10.1.20.204:8009;
}

server {
    listen 8009 http2;
    status_zone server_grpcgreeting;

    location / {
        status_zone location_grpcgreeting;
       # grpc_pass grpc://grpcgreeting_service;
       grpc_pass 10.1.20.204:8009;
    }
}
2. Start NGINX
~]# systemctl restart nginx

~]# netstat -antulop | grep nginx
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN      1948/nginx: master   off (0.00/0/0)
3. gRPC Client communicate gRPC server via NGINX
$ echo "ADDRESS=10.1.20.203:8009" > list/env-nginx.list

$ docker run --env-file ./list/env-nginx.list cloudadc/grpc-go-greeting:0.1 greeter_client
2023/02/28 02:53:33 Greeting: Hello world

$ docker run --env-file ./list/env-nginx.list cloudadc/grpc-go-greeting:0.1 greeter_client "Kylin SONG"
2023/02/28 02:58:30 Greeting: Hello Kylin SONG

$ docker run --env-file ./list/env-nginx.list cloudadc/grpc-go-greeting:0.1 greeter_client "his should be blocked <script>evil_script()</script>"
2023/02/28 03:01:18 Greeting: Hello his should be blocked <script>evil_script()</script>
Note
The gRPC server run on 10.1.20.204:8009, the NGINX run on 10.1.20.203:8009, the gRPC client run on localhost.

NGINX in front of gRPC Server, App Protect Policy on NGINX

Java

Build and Run on Local