Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Created June 28, 2022 15:01
Show Gist options
  • Save ammarfaizi2/235e3eddfbdf02da4664c081b79711eb to your computer and use it in GitHub Desktop.
Save ammarfaizi2/235e3eddfbdf02da4664c081b79711eb to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include "base/threading/thread.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/message_loop/message_pump_type.h"
#include "net/base/request_priority.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request.h"
#include "net/base/io_buffer.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_config_service_fixed.h"
#endif
namespace net {
constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("reporting", R"(
semantics {
sender: "Reporting API aaaaaaaa"
description:
"The Reporting API reports various issues back to website owners "
"to help them detect and fix problems."
trigger:
"Encountering issues. Examples of these issues are Content "
"Security Policy violations and Interventions/Deprecations "
"encountered. See draft of reporting spec here: "
"https://wicg.github.io/reporting."
data: "Details of the issue, depending on issue type."
destination: OTHER
}
policy {
cookies_allowed: NO
setting: "This feature cannot be disabled by settings."
policy_exception_justification: "Not implemented."
})");
class VisitURLDelegate: public URLRequest::Delegate {
int OnConnected(URLRequest* request,
const TransportInfo& info,
CompletionOnceCallback callback) override
{
puts(__func__);
return 0;
}
void OnReceivedRedirect(URLRequest* request,
const RedirectInfo& redirect_info,
bool* defer_redirect) override
{
puts(__func__);
}
void OnAuthRequired(URLRequest* request, const AuthChallengeInfo& auth_info)
override
{
puts(__func__);
}
void OnCertificateRequested(URLRequest* request,
SSLCertRequestInfo* cert_request_info) override
{
puts(__func__);
}
void OnSSLCertificateError(URLRequest* request, int net_error,
const SSLInfo& ssl_info, bool fatal) override
{
puts(__func__);
}
void OnResponseStarted(URLRequest* request, int net_error) override
{
puts(__func__);
}
void OnReadCompleted(URLRequest* request, int bytes_read) override
{
puts(__func__);
}
};
class CBProxy {
public:
CBProxy();
void Init();
void Run();
private:
static void VisitURL(std::string url);
static void SetUpOnNetworkThread(
std::unique_ptr<net::URLRequestContext>* context,
base::WaitableEvent* initialization_complete_event);
base::Thread net_thread_;
};
CBProxy::CBProxy():
net_thread_("network_thread") {
}
void CBProxy::Init() {
base::ThreadPoolInstance::InitParams p(256);
base::ThreadPoolInstance::Create("cbproxy");
base::ThreadPoolInstance::Get()->Start(p);
base::Thread::Options options(base::MessagePumpType::IO, 0);
CHECK(net_thread_.StartWithOptions(std::move(options)));
}
void CBProxy::VisitURL(std::string target_url) {
net::URLRequestContextBuilder uctx_b;
std::unique_ptr<net::URLRequestContext> uctx;
std::unique_ptr<URLRequest> u;
uctx_b.set_user_agent("Test Server");
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// On Linux, use a fixed ProxyConfigService, since the default one
// depends on glib.
//
// TODO(akalin): Remove this once http://crbug.com/146421 is fixed.
uctx_b.set_proxy_config_service(
std::make_unique<net::ProxyConfigServiceFixed>(
net::ProxyConfigWithAnnotation()));
#endif
auto buf = base::MakeRefCounted<net::IOBufferWithSize>(4096);
GURL url(target_url);
VisitURLDelegate del;
uctx = uctx_b.Build();
u = uctx->CreateRequest(url, IDLE, &del, traffic_annotation, false);
u->Start();
// u->Read(buf.get(), 4096);
printf("test!!!\n");
sleep(100);
}
void CBProxy::Run() {
std::string url = "http://127.0.0.1:8000/test.php";
net_thread_.task_runner()->PostTask(FROM_HERE,
base::BindOnce(CBProxy::VisitURL, url));
printf("after post task!!\n");
sleep(100);
}
} /* namespace net */
int main(int argc, const char *argv[]) {
base::CommandLine::Init(argc, argv);
net::CBProxy cbp;
cbp.Init();
cbp.Run();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment