Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
http_listener.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
42#pragma once
43
45
46#include <atomic>
47#include <chrono>
48#include <filesystem>
49#include <memory>
50#include <mutex>
51#include <string>
52#include <thread>
53
54namespace entropic {
55
56class ExternalBridge;
57
58namespace http {
59
66ENTROPIC_EXPORT std::string generate_token();
67
76ENTROPIC_EXPORT bool tokens_equal(const std::string& a, const std::string& b);
77
86ENTROPIC_EXPORT bool host_allowed(const std::string& host, int port);
87
95ENTROPIC_EXPORT std::string bearer_token(const std::string& header);
96
104ENTROPIC_EXPORT std::filesystem::path token_path_for(int port);
105
114ENTROPIC_EXPORT bool prepare_private_dir(const std::filesystem::path& dir, std::string& err);
115
125ENTROPIC_EXPORT bool write_token_file(const std::filesystem::path& path,
126 const std::string& token, std::string& err);
127
137class ENTROPIC_EXPORT AuthThrottle {
138public:
139 using Clock = std::chrono::steady_clock;
140
147 bool blocked(Clock::time_point now) const;
148
154 void record_failure(Clock::time_point now);
155
156private:
157 mutable std::mutex mu_;
158 int failures_ = 0;
159 Clock::time_point window_start_{};
160 Clock::time_point blocked_until_{};
161};
162
172class ENTROPIC_EXPORT HttpListener {
173public:
180 HttpListener(ExternalBridge* bridge, int port);
181
184
185 HttpListener(const HttpListener&) = delete;
186 HttpListener& operator=(const HttpListener&) = delete;
187
194 bool start(std::string& err);
195
200 void stop();
201
207 int port() const { return port_.load(); }
208
214 const std::filesystem::path& token_file() const { return token_file_; }
215
217 struct Impl;
218
219private:
226 std::string publish_token(int bound);
227
233 std::string await_running();
234
235 std::unique_ptr<Impl> impl_;
236 ExternalBridge* bridge_;
237 std::atomic<int> port_{0};
238 std::filesystem::path token_file_;
239 std::thread thread_;
240};
241
242} // namespace http
243} // namespace entropic
Unix socket MCP bridge for external client access.
Refuses after repeated authentication failures (decision #84).
Streamable-HTTP front end for an ExternalBridge (gh#204).
int port() const
The port actually bound.
const std::filesystem::path & token_file() const
Where this listener's token was written.
Symbol visibility macro for all exported symbols.
std::string err
size_exceeded, when set
std::filesystem::path token_path_for(int port)
Where the token for a port is written.
bool host_allowed(const std::string &host, int port)
Whether a Host header names this listener's loopback endpoint.
bool write_token_file(const std::filesystem::path &path, const std::string &token, std::string &err)
Write token to path atomically (temp + rename), mode 0600.
bool tokens_equal(const std::string &a, const std::string &b)
Compare two tokens in time independent of where they differ.
bool prepare_private_dir(const std::filesystem::path &dir, std::string &err)
Create or verify the token directory: 0700, ours, not a symlink.
std::string generate_token()
A fresh bearer token: 256 bits from getrandom(), hex-encoded.
std::string bearer_token(const std::string &header)
The token in an Authorization: Bearer <t> header, or "".
Activate model on GPU (WARM → ACTIVE).
@ optional
optional: every escape asks the host's path approver
std::string port
–port N
Definition serve.cpp:39