我正在尝试验证我的字符串是否与模式匹配.也就是说,完整的字符串可以写为该模式.但是,如果任何子字符串匹配该模式,则preg_match返回true.(例如,preg_match("#[a-z]*#, "333k")返回1,我不想.在这个例子中我宁愿验证,整个字符串只包含小拉丁字母.)
我想监视给另一个应用程序的关键事件,每当按下ctrl + d时,发送ctrl + c,抓取(新)剪贴板内容并依赖它执行其他操作.
我的代码:
void PressKeyboardKey(char key){
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = key;
ip.ki.wScan = 0;
ip.ki.dwFlags = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ){
char pressedKey;
// Declare a pointer to the KBDLLHOOKSTRUCTdsad
KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
switch( wParam )
{
case WM_KEYUP:
{
pressedKey = (char)pKeyBoard->vkCode; //get the key code
if ((pressedKey == …Run Code Online (Sandbox Code Playgroud) 我想使用golang创建一个文件的硬链接.os.Link()告诉我,不支持windows.因此我尝试使用os.exec,调用"mklink.exe".
cmd := exec.Command("mklink.exe", "/H", hardlink_path, file_path)
err := cmd.Run()
Run Code Online (Sandbox Code Playgroud)
但是,它告诉我,它无法在%PATH%中找到mklink.exe.这让我感到困惑,因为我可以使用cmd来调用它.
接下来我试图通过cmd间接调用它:
cmd := exec.Command("cmd.exe", "mklink.exe", "/H", hardlink_path, file_path)
err := cmd.Run()
Run Code Online (Sandbox Code Playgroud)
现在它不会返回任何错误,但是,它也不会创建硬链接.有什么建议?
我目前正在学习 Rust,为此我想创建自己的箱子并使用它。然而,Rust 找不到这个箱子。
\n我有以下文件结构:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80minimal\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80Cargo.toml\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80main.rs\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80util\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80win\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80Cargo.toml\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80lib.rs\nRun Code Online (Sandbox Code Playgroud)\n在最小文件夹中文件夹中我有我的主要项目。它看起来像这样:
\n[package]\nname = "minimal"\nversion = "0.1.0"\n[dependecies]\nwin = { path = "../util/win"}\nRun Code Online (Sandbox Code Playgroud)\nextern crate win; // ERROR: "Can\'t find crate for \'win\' rustc(E0463)"\nfn main() {\n println!("Hello, World!");\n}\nRun Code Online (Sandbox Code Playgroud)\n我的库位于win文件夹中如下所示:
\n[package]\nname = "win"\nversion = "0.1.0"\nRun Code Online (Sandbox Code Playgroud)\npub type TestType = String;\nRun Code Online (Sandbox Code Playgroud)\n我的第一个假设是我在指定Cargo.toml依赖项中的路径时犯了一个错误。所以我试着稍微扭动它,但似乎不起作用。
\n铁锈报告
\n\n找不到“win” …
该文件提供了一个例子-不幸的是它不会编译; 很多东西都被重命名了,ClientSession构造函数的接口也发生了变化。我设法将错误修复到可以编译的程度,但不能修复到可以工作的程度。
这是我使最小示例工作的最佳尝试:
extern crate rustls;
use io::Read;
use io::Write;
use rustls::Session;
use std::io;
fn main() {
let mut socket = std::net::TcpStream::connect("www.google.com:443").unwrap();
let mut config = rustls::ClientConfig::new();
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
let arc = std::sync::Arc::new(config);
let dns_name = webpki::DNSNameRef::try_from_ascii_str("www.google.com").unwrap();
let mut client = rustls::ClientSession::new(&arc, dns_name);
client.write(b"GET https://www.google.com\r\n\r\n").unwrap();
loop {
if client.wants_read() {
client.read_tls(&mut socket).unwrap();
client.process_new_packets().unwrap();
let mut plaintext = Vec::new();
client.read_to_end(&mut plaintext).unwrap();
io::stdout().write(&plaintext).unwrap();
}
if client.wants_write() {
client.write_tls(&mut socket).unwrap();
}
// For testing purposes only
std::thread::sleep_ms(1000);
} …Run Code Online (Sandbox Code Playgroud) 我试图smtp.SendMail()在go程序中使用。但是它会阻塞,直到超时才返回。这使我无法找出问题所在。
我的代码:
to := []string{"recepient@example.com"}
err := smtp.SendMail("smtp.web.de:25", smtp.CRAMMD5Auth("example@web.de", "password"), "example@web.de", to, []byte("hi"))
if err != nil {
fmt.Println(err)
}else{
fmt.Println("Success")
}
Run Code Online (Sandbox Code Playgroud)
长时间后,出现以下错误:
to := []string{"recepient@example.com"}
err := smtp.SendMail("smtp.web.de:25", smtp.CRAMMD5Auth("example@web.de", "password"), "example@web.de", to, []byte("hi"))
if err != nil {
fmt.Println(err)
}else{
fmt.Println("Success")
}
Run Code Online (Sandbox Code Playgroud)
有什么想法可能是真正的问题吗?
我正在构建一个使用 websockets 的服务器。
目前,每个连接的客户端都使用两个 goroutine。一本用于阅读,一本用于写作。编写 goroutine 基本上会监听它应该发送的消息的通道,然后尝试传递它们。
type User struct{
send chan []byte
...
}
func (u *User) Send(msg []byte){
u.send <- msg
}
Run Code Online (Sandbox Code Playgroud)
问题是,从客户端 A 读取可能会导致写入客户端 B。假设到 B 的连接有一些问题(例如非常慢)并且它的发送通道已经满了。当前的行为是,尝试向通道添加消息现在开始阻塞,直到从通道中删除某些内容。这意味着,现在 A 等待直到 B 的缓冲区不再满。
我想像这样解决它:
func (u *User) Send(msg []byte) err{
u.send, err <- msg
if err != nil{
//The channels buffer is full.
//Writing currently not possible.
//Needs appropriate error handling.
return err
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想要错误处理而不是阻塞,以防缓冲区已满。我如何做到最好?
我已经阅读了一些关于 C++ 中 printf() 的安全性。例如可以在这里找到示例。这让我想知道fmt.Printf()来自 golang 是否安全。更具体地说,如果格式化字符串本身可以伪造是否安全。
inputString := "String from user"
x := "test"
fmt.Printf(inputString, x, 15)
Run Code Online (Sandbox Code Playgroud)
当试图从 C++ 复制漏洞时,golang 似乎并不容易受到攻击。
例如fmt.Printf("%s%s%s%s%s%s%s%s%s%s%s%s\n"),不会使 golang 中的程序崩溃。
当然,这样的分析并不能证明这在 golang 中是安全的。所以我想在这里问:go 的开发人员是否对其 printf 函数进行了万无一失?
编辑:通过万无一失,我的意思是它没有任何意外的副作用。我当然希望得到的字符串完全受到损害。我不希望用户能够获得特权信息(例如未传递给 printf 的变量内容),或者用户能够写入任何内存(例如,为 x 分配新值)。
我想使用 Collidable 接口实现一个碰撞库
type Collidable interface{
BoundingBox() (float64,float64,float64,float64)
FastCollisionCheck(c2 Collidable) bool
DoesCollide(c2 Collidable) bool
Collide(c2 Collidable)
}
Run Code Online (Sandbox Code Playgroud)
它具有预定义的形状,例如。
type Circle struct{
X,Y,Radius float64
}
Run Code Online (Sandbox Code Playgroud)
这个想法是我可以做到
type Rock struct{
collision.Circle
....
}
Run Code Online (Sandbox Code Playgroud)
然后实现接口 Collidable,所以我可以将它传递给一个空间哈希映射(期望一个可碰撞的)。唯一需要做的就是根据我的需要覆盖 Collide() 函数。
但是type circle中的函数不能处理type rock,即使是强硬的它嵌入了一个圆圈。
func (c1 *Circle) DoesCollide(i Collidable) bool{
switch c2 := value.(type) {
case Circle:
//doesn't fire, as it is of type Rock (unknown in this package)
//Needed is something like
//if i_embeds_Circle then c2 := i_to_Circle
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?有没有更好的办法?
我想通过 golang 制作一个自定义的网络服务器。它需要 root 才能绑定到端口 80。但是我想尽快删除 root。
syscall.SetUid()根据票证 #1435返回“不支持” 。
我总是可以通过 iptables 将端口 80 重新路由到其他地方,但是这会打开任何非 root 进程以伪装成我的网络服务器 - 我不希望这是可能的。
我如何放弃我的应用程序的权限(或者干净地解决这个问题)。
我最近注意到你可以做类似的事情
void foo(){ }
//...
std::cout<<foo<<std::endl;
Run Code Online (Sandbox Code Playgroud)
无论传递的函数如何,这都会打印出"1".
现在我很好奇:这究竟是做什么的?