-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServer.cs
More file actions
99 lines (83 loc) · 2.92 KB
/
Server.cs
File metadata and controls
99 lines (83 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;
using Mono.Unix.Native;
using System;
using System.IO;
namespace Fnproject.Fn.Fdk
{
sealed internal class Server
{
internal static string SOCKET_PATH { get; private set; }
internal static string PHONY_SOCKET_PATH { get; private set; }
public Server()
{
var FN_LISTENER = System.Environment.GetEnvironmentVariable("FN_LISTENER");
var FN_FORMAT = System.Environment.GetEnvironmentVariable("FN_FORMAT");
if (string.IsNullOrEmpty(FN_LISTENER) ||
!FN_LISTENER.StartsWith("unix:"))
{
throw new ArgumentException("Malformed FN_LISTENER");
}
if (!string.IsNullOrEmpty(FN_FORMAT) &&
FN_FORMAT != "http-stream")
{
throw new ArgumentException("Unsupported FN_FORMAT");
}
Uri url = new Uri(FN_LISTENER);
var socketPath = url.AbsolutePath;
var socketDir = Path.GetDirectoryName(socketPath);
var symlinkFileName = $"phony-{Path.GetFileName(socketPath)}";
var symlinkSocketPath = Path.Join(Path.GetDirectoryName(socketPath), symlinkFileName);
SOCKET_PATH = socketPath;
PHONY_SOCKET_PATH = symlinkSocketPath;
}
internal static void SocketPermissions(string phonySock, string realSock)
{
if (Syscall.chmod(
phonySock,
NativeConvert.FromOctalPermissionString("0666")
) < 0)
{
var error = Stdlib.GetLastError();
throw new ArgumentException("Error setting file permissions: " + error);
}
if (Syscall.symlink(Path.GetFileName(phonySock), realSock) < 0)
{
var error = Stdlib.GetLastError();
throw new ArgumentException("Error creating symlink: " + error);
}
}
internal static void DeleteStaleSockets()
{
if (File.Exists(SOCKET_PATH))
{
File.Delete(SOCKET_PATH);
}
if (File.Exists(PHONY_SOCKET_PATH))
{
File.Delete(PHONY_SOCKET_PATH);
}
}
private IHost newPrepareServer()
{
return Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseStartup<Startup>()
.UseKestrel(opt =>
{
DeleteStaleSockets();
opt.ListenUnixSocket(PHONY_SOCKET_PATH);
});
})
.Build();
}
public void Run()
{
var server = this.newPrepareServer();
server.Run();
}
}
}