-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added capability for only the whitelisted ips to connect to the subsc…
…riber
- Loading branch information
Showing
5 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/nuklai/nuklaivm-external-subscriber/config" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/peer" | ||
) | ||
|
||
var WhitelistedIPs = make(map[string]bool) | ||
|
||
// LoadWhitelist loads the whitelist using the config package | ||
func LoadWhitelist() { | ||
ips := config.GetWhitelistIPs() | ||
if len(ips) == 0 { | ||
log.Println("No whitelisted IPs provided. The gRPC server will reject all connections.") | ||
return | ||
} | ||
|
||
// Populate the whitelist map | ||
for _, ip := range ips { | ||
WhitelistedIPs[ip] = true | ||
} | ||
|
||
log.Printf("Loaded whitelisted IPs: %v\n", WhitelistedIPs) | ||
} | ||
|
||
// UnaryInterceptor checks the IP of the client and allows/denies the connection | ||
func UnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
peerInfo, ok := peer.FromContext(ctx) | ||
if !ok { | ||
return nil, fmt.Errorf("could not retrieve peer info") | ||
} | ||
|
||
clientIP := strings.Split(peerInfo.Addr.String(), ":")[0] // Extract IP address | ||
if !WhitelistedIPs[clientIP] { | ||
log.Printf("Unauthorized connection attempt from IP: %s", clientIP) | ||
return nil, fmt.Errorf("unauthorized IP: %s", clientIP) | ||
} | ||
|
||
return handler(ctx, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters