Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
@percybolmer
percybolmer / certgen.sh
Last active March 5, 2021 21:48
Certificate generation script
#!/bin/bash
# generate ca.key
openssl genrsa -out ca.key 4096
# generate certificate
openssl req -new -x509 -key ca.key -sha256 -subj "/C=SE/ST=HL/O=Example, INC." -days 365 -out ca.cert
# generate the server key
openssl genrsa -out server.key 4096
# Generate the csr
openssl req -new -key server.key -out server.csr -config certificate.conf
@percybolmer
percybolmer / GenerateTLSApi.go
Last active August 23, 2021 11:41
Generates a TLS grpc server by reading a Cert and Key
// GenerateTLSApi will load TLS certificates and key and create a grpc server with those.
func GenerateTLSApi(pemPath, keyPath string) (*grpc.Server, error) {
cred, err := credentials.NewServerTLSFromFile(pemPath, keyPath)
if err != nil {
return nil, err
}
s := grpc.NewServer(
grpc.Creds(cred),
)
@percybolmer
percybolmer / User.proto
Last active December 22, 2020 14:47
A example proto of a user service
syntax = "proto3";
package main;
option go_package=".;pingpong";
message PingRequest {
}
message PongResponse {
bool ok = 1;
}
// PingPongServer is the server API for PingPong service.
// All implementations must embed UnimplementedPingPongServer
// for forward compatibility
type PingPongServer interface {
Ping(context.Context, *PingRequest) (*PongResponse, error)
mustEmbedUnimplementedPingPongServer()
}
@percybolmer
percybolmer / Ping Server.go
Last active August 6, 2021 11:34
Fulfilling PingPong interface
package main
import (
"context"
pingpong "github.com/percybolmer/grpcexample/pingpong"
)
// Server is the Logic handler for the server
// It has to fullfill the GRPC schema generated Interface
@percybolmer
percybolmer / Only GRPC main.go
Last active August 6, 2021 11:35
A regular GRPC example
package main
import (
"log"
"net"
"time"
pingpong "github.com/percybolmer/grpcexample/pingpong"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
@percybolmer
percybolmer / grpcMultiplexer.go
Last active August 6, 2021 11:35
Grpc -> Htttp Multipelxer middleware
type grpcMultiplexer struct {
*grpcweb.WrappedGrpcServer
}
// Handler is used to route requests to either grpc or to regular http
func (m *grpcMultiplexer) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if m.IsGrpcWebRequest(r) {
m.ServeHTTP(w, r)
return
// loadTLSCfg will load a certificate and create a tls config
func loadTLSCfg() *tls.Config {
b, _ := ioutil.ReadFile("../cert/server.crt")
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
log.Fatal("credentials: failed to append certificates")
}
config := &tls.Config{
InsecureSkipVerify: false,
RootCAs: cp,
@percybolmer
percybolmer / GRPC tls Client calling Ping.go
Last active August 6, 2021 11:35
a simple main function that loads tls connection and calls ping
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"github.com/percybolmer/grpcexample/pingpong"
func main() {
// We Generate a TLS grpc API
apiserver, err := GenerateTLSApi("cert/server.crt", "cert/server.key")
if err != nil {
log.Fatal(err)
}
// Start listening on a TCP Port
lis, err := net.Listen("tcp", "127.0.0.1:9990")
if err != nil {