6

My organization runs its own GitHub server and web proxies. I have configured git so that I can work with both github.com and our internal GitHub from the command line. But using LibGit2Sharp, I cannot perform operations against our GitHub server. The only callback from CloneOptions that is invoked is RepositoryOperationStarting. No additional callbacks are invoked. I've posted the relevant code and configuration below (names have been changed to preserve anonymity). I am using LibGit2Sharp v0.25.2 from NuGet.

Code using LibGit2Sharp. Comments indicate which callbacks fire when hitting our internal github. When hitting github.com, all callbacks are invoked as expected.

private static void Main(string[] args)
{
    var options = new CloneOptions
    {
        CertificateCheck = (certificate, valid, host) => true, // never called
        CredentialsProvider = (url, fromUrl, types) => null, // never called
        OnCheckoutProgress = (path, steps, totalSteps) => { }, // never called
        OnProgress = output => true, // never called
        OnTransferProgress = progress => true, // never called
        OnUpdateTips = (name, id, newId) => true, // never called
        RepositoryOperationCompleted = context => { }, // never called
        RepositoryOperationStarting = context => true // ONLY THIS ONE IS CALLED
    };

    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; // never called

    Repository.Clone(args[0], args[1], options);
}

Exception:

Unhandled Exception: LibGit2Sharp.LibGit2SharpException: failed to send request: The operation timed out
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in C:\projects\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 136
   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts) in C:\projects\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 354
   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options) in C:\projects\libgit2sharp\LibGit2Sharp\Repository.cs:line 715
   at gitex.Program.Main(String[] args) in D:\dev\mgunter\gitex\gitex\Program.cs:line 71

Cloning from the command-line works:

C:\> git clone https://github.my-domain.com/organization/project
Cloning into 'project'...
remote: Counting objects: 1373, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 1373 (delta 8), reused 0 (delta 0), pack-reused 1350R
Receiving objects: 100% (1373/1373), 383.10 KiB | 0 bytes/s, done.
Resolving deltas: 100% (862/862), done.

Here are the relevant pieces of my git config. I have also tried with HTTP_PROXY and HTTPS_PROXY environment variables, but no luck.

[http "https://github.my-domain.com"]
    proxy = http://proxy-for-internal.my-domain.com:80
[http "https://github.com"]
    proxy = http://proxy-for-external.my-domain.com:81
    sslCAinfo = ~/certificates/my-domain-root-ca.cer
[credential]
    helper = wincred
[hub]
    host = github.my-domain.com
    protocol = https

Using WireShark, I see that command-line git does indeed hit my proxy server. However, my .NET program using LibGit2Sharp does not hit the proxy server at all.

Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • Maybe passing user name and credential with clone options `var co = new CloneOptions();` – Hackerman May 11 '17 at 20:01
  • Yu follow this example: `var co = new CloneOptions(); co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = "Username", Password = "Password" }; Repository.Clone("https://github.com/libgit2/libgit2sharp.git", "path/to/repo", co);` – Hackerman May 11 '17 at 20:09
  • Also I don't see the `.git` on your code – Hackerman May 11 '17 at 20:10
  • Weird...let me take a look at this once I got home...I am going to create a sample project in order to help you with this issue – Hackerman May 11 '17 at 20:14
  • I am looking at the tests https://github.com/libgit2/libgit2sharp/blob/master/LibGit2Sharp.Tests/CloneFixture.cs – Hackerman May 11 '17 at 21:06
  • Please note that the comments above are all outdated. I deleted comments that I had made in response to these questions. All relevant information is now in the post. – Michael Gunter Oct 03 '18 at 22:46
  • @MichaelGunter Have you considered checking `ServicePointManager` related callback in case it is a certificate issue? – Nkosi Oct 03 '18 at 23:27
  • @Nkosi I was hopeful, but no. I tried `ServicePointManager.ServerCertificateValidationCallback`, but it's not invoked either. – Michael Gunter Oct 04 '18 at 16:02
  • 1. You could run Wireshark and capture the traffic to make sure the ports are hit correctly. If it works in commandline but not in code, I would guess the communication is incorrect via code. What packets are sent via commandline? What packets are sent via your code and the lib? That might give you a hint as to what is missing in your code. – Rhyous Oct 09 '18 at 14:04
  • @Rhyous I'm a little out of my element here, but from what I can tell from WireShark, LibGit2Sharp is not using the proxy server at all when hitting my internal github. Using git from the command line, it does get hit. – Michael Gunter Oct 12 '18 at 21:45
  • @Hackerman Any other thoughts here? It appears LibGit2Sharp just isn't honoring the proxy information in `.gitconfig`. – Michael Gunter Oct 12 '18 at 21:55
  • 1
    @MichaelGunter Then there is your answer. I googled LibGit2Sharp and found this: https://github.com/libgit2/libgit2sharp/issues/1429 If LibGit2Sharp isn't handling a proxy, then you have to code that yourself. It looks like LibGit2Sharp is just incomplete or has a bug. It seems it has code to read the proxy, just it isn't using it. You just need a method to check if a proxy is defined and if so, use it. Probably not what you want to here, but your best bet is to pull LibGit2Sharp from GitHub, compile and debug it, and start a dialogue with the project owners. – Rhyous Oct 15 '18 at 18:49

1 Answers1

0

Any other thoughts here? It appears LibGit2Sharp just isn't honoring the proxy information in .gitconfig

That seems to be the case, from libgit2/libgit2sharp issue 1429, where Brandon Ording (bording) comments:

It looks like GitProxyOptions were added in 88cf9a7, and they are being used in Commands.Fetch, as you can see here.

However, it appears that currently everywhere in the codebase, the GitProxyType of GitProxyOptions is always being initialized to 0, which is None.
Looking at the libgit2 code for git_proxy_t, it appears that None disables using any proxies.

Edward Thomson (ethomson) confirms:

Yes, it does look like this is disabling proxies... The default transport on Windows (WinHTTP) does not support proxies, IIRC, so this is basically a noop there. But turning on auto support would help when libgit2 itself is built with libcurl support.

So: commit 0d72f67f2 does mention:

proxy: don't specify the protocol in the type

We leave this up to the scheme in the url field.
The type should only tell us about whether we want a proxy and whether we want to auto-detect it.

That was in response to issue 3110, which involved a month later commit 1dc4491, with a proxy configuration example, but with libgit2 and a Java program.

HTTP(S)_PROXY should be picked up though, but: for an internal service like your own GitHub server, do check if you actually need a proxy.
Most often, you would set NO_PROXY=localhost,.mycompany.com in order to not use any proxy when contacting an internal (LAN instead of WAN) server.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250