简体   繁体   English

将Java程序之间的通信限制为同一用户

[英]Restrict communication between Java programs to same user

We want to build a Java app associated with files and URLs, but only one instance should run at a time. 我们想构建一个与文件和URL关联的Java应用程序,但是一次只能运行一个实例。 If a second file is opened, it should be handled by an already running instance, if available. 如果打开了第二个文件,则应由一个正在运行的实例处理(如果有)。 (Think of this as a file viewer: a user clicking a link in a browser opens the app, clicking a second link brings the same app to the front and opens the second URL in a new view.) (将其视为文件查看器:用户单击浏览器中的链接将打开该应用程序,单击第二个链接会将同一应用程序置于最前面,并在新视图中打开第二个URL。)

To that end, my plan was: 为此,我的计划是:

  • User calls app file1.ext . 用户调用app file1.ext This starts the first instance. 这将启动第一个实例。
  • This instance detects it is the only instance running, continues launch and opens file1.ext . 该实例检测到它是唯一正在运行的实例,继续启动并打开file1.ext
  • User calls app file2.ext . 用户调用app file2.ext This starts the second instance. 这将启动第二个实例。
  • Second instance detects it is the second instance and sends a command to open file2.ext to the first instance. 第二个实例检测到它是第二个实例,并发送命令打开file2.ext到第一个实例。
  • First instance receives that command from second instance, opens file2.ext , and confirms. 第一个实例从第二个实例接收该命令,打开file2.ext并进行确认。
  • Second instance aborts launch and exits. 二审中止启动并退出。

I can think of a variety of technologies for implementing the communication channel, but all of them open a port on the local machine. 我可以想到各种用于实现通信通道的技术,但是所有这些技术都会在本地计算机上打开端口。 That is fine, but I want to make sure only the current user can use this port. 很好,但是我想确保只有当前用户才能使用此端口。 I can probably prevent a second user from sending commands to the first user's app accidentally, but how do I enforce that a port or whatever is used for the inter-process communication is available only to the user who initiates the first app launch? 我可能可以防止第二个用户意外地将命令发送到第一个用户的应用程序,但是如何强制端口或用于进程间通信的任何内容仅对启动第一个应用程序的用户可用?

The solution should be as platform independent as possible. 解决方案应尽可能独立于平台。

You can use a lock/pid file, the program should check if the lock file exists to find if there is an existing process. 您可以使用锁/ PID文件,程序应检查锁文件是否存在以查找是否存在现有进程。 The lock file should contain the port number it's listening on, the server key, and a session key. 锁定文件应包含正在侦听的端口号,服务器密钥和会话密钥。 You'll need to set the file permission so it's only readable by the current user . 您需要设置文件许可权,以便当前用户只能读取它 All messages to the initial process must contain a matching session key. 发送给初始进程的所有消息都必须包含匹配的会话密钥。 The session key is the proof that the originating process has the permission to read the lock file, so your open port inherits the lock file's permission. 会话密钥证明了原始进程具有读取锁定文件的权限,因此您的打开端口会继承锁定文件的权限。

For security, you'll need to be careful about the order that you open the port and write the lock file. 为了安全起见,您需要注意打开端口并写入锁定文件的顺序。 You'll need to make sure that you have the port open before writing the lock file , or a malicious program could outrace the server and receive messages that it shouldn't have been able to read. 在编写锁定文件之前 ,您需要确保已打开端口 ,否则恶意程序可能会超出服务器的接收范围,并接收其不应该读取的消息。 Also, the second process should check the owner of the lock file , to make sure that it's created by the current user. 另外,第二个过程应该检查锁文件的所有者 ,以确保它是由当前用户创建的。 Also, before the second processes start sending in data, you should also check the server return a server key in its handshake, to make sure that the currently running server actually also have read access to the lock file, as the server could have been long dead, replaced by the malicious program. 另外,在第二个进程开始发送数据之前,您还应该检查服务器握手时是否返回了服务器密钥 ,以确保当前正在运行的服务器实际上也对锁定文件具有读访问权限,因为服务器可能很长。死机,由恶意程序代替。 Finally, make sure that the program binds only to local connections , unless you actually want to allow requests from the network. 最后,除非您实际上希望允许来自网络的请求,否则请确保该程序仅绑定到本地连接

If this is a Linux/Unix/Mac only program, then you have another alternative of opening a Unix domain socket. 如果这是仅适用于Linux / Unix / Mac的程序,那么您还有另一种选择来打开Unix域套接字。 You should set the file permission for the domain socket so that it's only readable/writable by the current user. 您应该设置域套接字的文件许可权,以便当前用户只能对其进行读写。 If you use domain socket, you don't need a session key or all these handshake things since a domain socket's permission is enforced by the domain socket's file permission. 如果您使用域套接字,则不需要会话密钥或所有这些握手操作,因为域套接字的权限由域套接字的文件权限强制执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM