Skip to content

Commit 44fc98c

Browse files
committed
feature: auto-detect HTTPS website for git@<host>:<repo> formatted remote (#1636)
Signed-off-by: leo <[email protected]>
1 parent 9ac6afa commit 44fc98c

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

src/Commands/QueryRemotes.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public QueryRemotes(string repo)
4040
if (outs.Find(x => x.Name == remote.Name) != null)
4141
continue;
4242

43+
if (remote.URL.StartsWith("git@", StringComparison.Ordinal))
44+
{
45+
var hostEnd = remote.URL.IndexOf(':', 4);
46+
if (hostEnd > 4)
47+
{
48+
var host = remote.URL.Substring(4, hostEnd - 4);
49+
Models.HTTPSValidator.Add(host);
50+
}
51+
}
52+
4353
outs.Add(remote);
4454
}
4555

src/Models/CommitLink.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public static List<CommitLink> Get(List<Remote> remotes)
4040
outs.Add(new($"Gitea ({route})", $"{link}/commit/"));
4141
else if (host.Equals("git.sr.ht", StringComparison.Ordinal))
4242
outs.Add(new($"sourcehut ({route})", $"{link}/commit/"));
43+
else if (host.Equals("gitcode.com", StringComparison.Ordinal))
44+
outs.Add(new($"GitCode ({route})", $"{link}/commit/"));
4345
}
4446
}
4547

src/Models/HTTPSValidator.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Security;
4+
using System.Net.Sockets;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace SourceGit.Models
9+
{
10+
public static class HTTPSValidator
11+
{
12+
public static void Add(string host)
13+
{
14+
lock (_syncLock)
15+
{
16+
// Already checked
17+
if (_hosts.ContainsKey(host))
18+
return;
19+
20+
// Temporarily mark as supported to avoid duplicate checks
21+
_hosts.Add(host, true);
22+
23+
// Well-known hosts always support HTTPS
24+
if (host.Contains("github.com", StringComparison.Ordinal) ||
25+
host.Contains("gitlab", StringComparison.Ordinal) ||
26+
host.Contains("azure.com", StringComparison.Ordinal) ||
27+
host.Equals("gitee.com", StringComparison.Ordinal) ||
28+
host.Equals("bitbucket.org", StringComparison.Ordinal) ||
29+
host.Equals("gitea.org", StringComparison.Ordinal) ||
30+
host.Equals("gitcode.com", StringComparison.Ordinal))
31+
return;
32+
}
33+
34+
Task.Run(() =>
35+
{
36+
var supported = false;
37+
38+
try
39+
{
40+
using (var client = new TcpClient())
41+
{
42+
client.ConnectAsync(host, 443).Wait(3000);
43+
if (!client.Connected)
44+
{
45+
client.ConnectAsync(host, 80).Wait(3000);
46+
supported = !client.Connected; // If the network is not available, assume HTTPS is supported
47+
}
48+
else
49+
{
50+
using (var ssl = new SslStream(client.GetStream(), false, (s, cert, chain, errs) => true))
51+
{
52+
ssl.AuthenticateAsClient(host);
53+
supported = ssl.IsAuthenticated; // Hand-shake succeeded
54+
}
55+
}
56+
}
57+
}
58+
catch
59+
{
60+
// Ignore exceptions
61+
}
62+
63+
lock (_syncLock)
64+
{
65+
_hosts[host] = supported;
66+
}
67+
});
68+
}
69+
70+
public static bool IsSupported(string host)
71+
{
72+
lock (_syncLock)
73+
{
74+
if (_hosts.TryGetValue(host, out var supported))
75+
return supported;
76+
77+
return false;
78+
}
79+
}
80+
81+
private static Lock _syncLock = new();
82+
private static Dictionary<string, bool> _hosts = new();
83+
}
84+
}

src/Models/Remote.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public bool TryGetVisitURL(out string url)
7878
var match = REG_TO_VISIT_URL_CAPTURE().Match(URL);
7979
if (match.Success)
8080
{
81-
url = $"https://{match.Groups[1].Value}/{match.Groups[2].Value}";
81+
var host = match.Groups[1].Value;
82+
var supportHTTPS = HTTPSValidator.IsSupported(host);
83+
var scheme = supportHTTPS ? "https" : "http";
84+
url = $"{scheme}://{host}/{match.Groups[2].Value}";
8285
return true;
8386
}
8487

0 commit comments

Comments
 (0)