Created
December 3, 2021 11:57
-
-
Save salrashid123/3833671de598268c6341aaf108754d23 to your computer and use it in GitHub Desktop.
gcp_proxy dotnet with basic sample (https://github.com/salrashid123/gcpsamples/tree/master/proxy)
This file contains hidden or 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
using System; | |
using Google.Cloud.Storage.V1; | |
using Google.Cloud.PubSub.V1; | |
using Google.Apis.Auth.OAuth2; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Google.Apis.Http; | |
using Google.Apis.Services; | |
using Google.Apis.Storage.v1; | |
using Google.Api.Gax.ResourceNames; | |
namespace main | |
{ | |
class Program | |
{ | |
const string projectID = "your-project"; | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
new Program().Run().Wait(); | |
} | |
private async Task Run() | |
{ | |
// 1. basic auth, with usercredentials | |
// need export http_proxy=http://user1:user1@localhost:3128 for pubsub | |
// configure ProxySupportedHttpClientFactory with auth for gcs, oauth2 | |
// auth Y | |
// gcs Y | |
// pubsub Y | |
// 1638363563.445 0 192.168.9.1 TCP_DENIED/407 3999 CONNECT oauth2.googleapis.com:443 - HIER_NONE/- text/html | |
// 1638363563.662 0 192.168.9.1 TCP_DENIED/407 4068 CONNECT storage.googleapis.com:443 - HIER_NONE/- text/html | |
// 1638363564.438 776 192.168.9.1 TCP_TUNNEL/200 45147 CONNECT storage.googleapis.com:443 user1 HIER_DIRECT/142.250.73.208 - | |
// 1638363564.438 345 192.168.9.1 TCP_TUNNEL/200 7877 CONNECT pubsub.googleapis.com:443 user1 HIER_DIRECT/142.250.73.202 - | |
// 1638363564.438 987 192.168.9.1 TCP_TUNNEL/200 7348 CONNECT oauth2.googleapis.com:443 user1 HIER_DIRECT/142.250.73.234 - | |
// NOTE, see deny first | |
// 2. no basic auth, with service account credentials | |
// export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc_account.json | |
// Invalid Credentials [401] | |
// Errors [ | |
// Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global] | |
// ] | |
// 1638363615.081 0 192.168.9.1 TCP_DENIED/407 4068 CONNECT storage.googleapis.com:443 - HIER_NONE/- text/html | |
// 1638363615.287 201 192.168.9.1 TCP_TUNNEL/200 8242 CONNECT storage.googleapis.com:443 user1 HIER_DIRECT/142.250.73.208 - | |
// 3. no basicauth, with ServiceAccountCredential | |
//var stream = new FileStream("/path/to/svc_account.json", FileMode.Open, FileAccess.Read); | |
//ServiceAccountCredential sacredential = ServiceAccountCredential.FromServiceAccountData(stream); | |
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync(); | |
credential = credential.CreateWithHttpClientFactory(new ProxySupportedHttpClientFactory()); | |
StorageService service = new StorageService(new BaseClientService.Initializer | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = StorageClientImpl.ApplicationName, | |
HttpClientFactory = new ProxySupportedHttpClientFactory(), | |
}); | |
var client = new StorageClientImpl(service, null); | |
foreach (var b in client.ListBuckets(projectID)) | |
Console.WriteLine(b.Name); | |
PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); | |
ProjectName projectName = ProjectName.FromProject(projectID); | |
foreach (Topic t in publisher.ListTopics(projectName)) | |
Console.WriteLine(t.Name); | |
} | |
} | |
public class ProxySupportedHttpClientFactory : HttpClientFactory | |
{ | |
protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args) | |
{ | |
ICredentials credentials = new NetworkCredential("user1", "user1"); | |
var proxy = new WebProxy("http://127.0.0.1:3128", true, null, credentials); | |
//var proxy = new WebProxy("http://127.0.0.1:3128", true, null, null); | |
var webRequestHandler = new HttpClientHandler() | |
{ | |
UseProxy = true, | |
Proxy = proxy, | |
UseCookies = false | |
}; | |
return webRequestHandler; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment